duanlu7223 2017-08-26 14:04
浏览 45

laravel 5.4 - 从路径向控制器注入对象

I have some routes like this.

Route::prefix('bots')->group(function () {
    Route::get('/', 'BotController@index')->name('bots');
    Route::get('{bot_username}', 'BotController@edit');
    ...
});

what I want is in any way Inject a Bot model object to the second route in order to edit it.

so if we go to

mydomain.dev/bots/mybotusername

it should automatically call a custom method like findByUsername that I will define in a repository or a if possible in the bot model itself, and it will return a bot model object. in this model I should pass the route parameter bot_username and find and make an object and return it. so if it IS returning the object we can access the route to edit the object in BotController@edit and if returned null we cant access the route and show 404 error. is it possible?

  • 写回答

0条回答 默认 最新

    报告相同问题?