Connect with us

Laravel 7 comes with Easy Implicit Route Model Binding

kizinho

Published

on

Follow
NEWS: Laravel 7 comes with Easy Implicit Route Model Binding [New  Developer] » Naijacrawl
Photo: Naijacrawl
site

Laravel 7 will be out 2020 with new awesome features. You can clean the way routing is been done with one line of code.

But you can try the dev version by running this code

laravel new example --dev


In laravel 7 you can bind routes like this:


Route::get('/posts/{post:slug}', function (Post $post) {
    // ...
});


Currently, Laravel 6 and below requires you to define a getRouteKeyName() method on the model like so:

<?php

class Post extends Model
{
    /**
     * Get the route key for the model.
     *
     * @return string
     */
    public function getRouteKeyName()
    {
        return 'slug';
    }
}


You can use the below route binding when you have multiple routes that you want to bind differently.

For example, the frontend route uses slugs to display posts and backend admin uses ids to manage posts:


Route::get('/posts/{post:slug}', function (Post $post) {
    // ...
});

// Or you could use the default `{post}` here...
Route::get('/admin/posts/{post:id}/edit', function (Post $post) {
    // ...
});


Thanks for reading.



Click Here To Comment


site


kizinho

I am a software developer, like meeting people and love blogging, that's why I developed naijacrawl because that's what I love doing.

Continue Reading
1 Comment

1 Comment

  1.  Timmortal

    Thanks for this amazing tutorial, kizhinho.
    There are so much i can take away from this and refractor some PHP that i have been working on, hope to see more from you soon.


Leave a Reply

Your email address will not be published. Required fields are marked *

How to show a nice message to the user when an error occurs in using Guzzle with Laravel

kizinho

Published

on

DEVELOPER PROGRAMMING: How to show a nice message to the user when an error occurs in using Guzzle with Laravel [New  Developer Programming] » Naijacrawl
Photo: Naijacrawl
Hi Folks, have you encountered an ugly Exception when making a post, get, put, delete to external API when an error like 500 or 404 occurs. Today you are at the right place, after reading this you will be able to show a nice message to your users in case of any of those errors occursWhen making a request to external API using guzzle to solve this problem you will need to catch the Exception by following the steps below.Prepare your request using Guzzle, I believe yo...
Continue Reading

Laravel 6 and 7 Package for Paystack

kizinho

Published

on

NEWS: Laravel  6 and  7 Package for Paystack [New  Developer] » Naijacrawl
Photo: Naijacrawl
Hi, Folks since the release of laravel version 6 and 7, it is very difficult to easily use Paystack on it due to lack of maintenance but today unicodeveloper of the package have taken his time to release a new update on the package.To use it, you just need to follow the same pattern we normally do and get things working back again.Visit the package URL here
Continue Reading

Latest