Skip to content

Instantly share code, notes, and snippets.

@gabbydgab
Created September 2, 2017 13:12
Show Gist options
  • Save gabbydgab/df35f753586cd1d3d1374b5d5aeed5a4 to your computer and use it in GitHub Desktop.
Save gabbydgab/df35f753586cd1d3d1374b5d5aeed5a4 to your computer and use it in GitHub Desktop.
Route Model Binding in Laravel Package
<?php
/**
* Following binding works!
*/
namespace MyPackage\Model;
class User
{
}
namespace App\Http\Controller;
class UserController
{
public function view(User $user)
{
dd($user);
}
}
// routes/web.php under App\Http\Controller
Route::get(/user/{user}, "UserController@view");
/**
* Following binding doesn't works!
*/
namespace MyPackage\Model;
class User
{
}
class UserController
{
public function view(User $user)
{
dd($user); // is null
}
}
// routes/web.php under MyPackage\Http\Controller
Route::get(/user/{user}, "UserController@view");
@James-N-M
Copy link

Add the following to your Service Provider files boot method.

// top of file
use Illuminate\Support\Facades\Route;
// inside boot method
Route::group([
            'middleware' => ['bindings'],
        ], function () {
            $this->loadRoutesFrom(__DIR__.'/../routes/web.php');
        });

@MoeinMDN
Copy link

thank you a lot by clarify the solution.
you solved one of my biggest challenges in making a package in the last few days.

Add the following to your Service Provider files boot method.

// top of file
use Illuminate\Support\Facades\Route;
// inside boot method
Route::group([
            'middleware' => ['bindings'],
        ], function () {
            $this->loadRoutesFrom(__DIR__.'/../routes/web.php');
        });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment