dongzhe6287 2017-06-19 06:47
浏览 67
已采纳

将发布的数据绑定到Laravel 5.4中的模型

I have seen other topics regarding this issue, didn't work out.

So in Laravel 5.4 Route Model Binding, we can bind a route to a model like:

define the route in web.php:

web.php:

Route::get('/users/{user}', UsersController@show);

UsersController@show:

public function show(User $user){
    // now we already have access to $user because of route model binding
    // so we don't need to use User::find($user), we just return it:
    return view(users.show, compact('user'));
}

The above code will work just fine, so in our controller we can return the $user without finding the user, we already have it.

but imagine this:

web.php:

Route::patch('/users/archive', UsersController@archive);

EDITED: now the above line makes a patch route and we don't have {user} in the route url, the user id is being posted via the form.

UsersController@archive:

public function archive(Request $request, User $user){
    // how can I access the $user here without using User::find($user);
    // I get to this action via a form which is posting `user` as a value like `5`
    dd($request->user); // this now echo `5`
    // I can do:
    // $user = User::find($request->user);
    // and it works, but is there a way to not repeat it every time in every action
}

What I have tried: in RouteServiceProvider::boot() I have:

Route::model('user', 'App\User');

The above is what i have found in Google, but not working.

I would appreciate any kind of help.


EDIT: It seems it's not called Route Model Binding anymore since we don't have the {user} in the route and that's because my code is not working, the user variable is being posted to the controller and it's only accessible via $request->user.

this is route model binding:

Route::patch('users/{user}/archive', UsersController@archive);

this is not:

Route::patch('users/archive', UsersController@archive);

since we don't have {user} and it's being posted via the form and could be accessed only via $request->user. (please correct me if I am wrong about the definition of route model binding)

SO:

what I want to achieve in a nutshell: in every request being sent to my UsersController, if I am sending user variable as a post variable, it must be bounded to User::findOrFail($request->user) and then $user must be available in my controller actions.

I want to achieve this because in every action I am repeating myself doing User::findOrFail($request->user) and I don't want to do that, so I want to check in every request if I have a variable name like a model name, they should be bounded.

  • 写回答

3条回答 默认 最新

  • drdyf42880 2017-06-21 14:19
    关注

    There's no need to bind explicitly to the User class, so Route::model('user', 'App\User'); should be removed; type-hinting should be enough instead.

    public function archive(Request $request, User $user) { ... }
    

    should be working, just make sure you are importing the right User class at the top of the file (use App\User;).

    Then the model is in your $user variable (method argument), try dd($user).


    It's clear now that since the {user} variable is not in the URI, this is not a route model binding issue. You just want the User instance injected as a parameter based on the contents of the request.

    $this->app->bind(User::class, function () {
        $user_id = request('user') ?: request()->route('user');
        return User::findOrFail($user_id);
    });
    

    You could add that to the register method in the AppServiceProvider (or any other registered provider) to have the model injected. I leave it to you to generalize this to other model classes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持