Laravel 5.2 implicit route model binding

In Laravel 5.2 you can use implicit route model binding to make your controllers even more clean.

Route::get('clients/{client}', function ($client) {
    return $client;
});

This does exactly what show method do in resource controllers – returns client which id is given id after /.

BUT there is one main problem. In your routes Auth facade still not working – Auth::user() and Auth::id() returns null. Also if you have global scope in your SaaS app something like:

class Client extends Model
{

    protected static function boot()
    {
        parent::boot();

        static::addGlobalScope('user_scope', function (Builder $builder) {
            $builder->where('user_id', Auth::id());
        });
    }

    protected $fillable = ['user_id'];
}

you don’t get results from your Client::class model because Auth::id() returns null.

So in general: go explicitly and do your stuff in controllers to avoid these “errors”.

Hope to save your time to deal with it.

Share on FacebookShare on RedditShare on Google+Tweet about this on TwitterShare on LinkedIn
don't waste your time using copy/paste, just use share buttons above
Laravel 5.2 implicit route model binding