无法在Laravel中以编辑表单检索旧数据

时间:2019-04-07 22:11:53

标签: php laravel laravel-5 crud laravel-resource

我想在按下编辑按钮时获取该ID的数据。我的所有字段都为空。我在同一页面上创建和编辑。

更新功能

price

编辑按钮

 public function update(Request $request)
{

    $user = User::findOrFail($request->user_id);

    $user->update($request->all());

    return back();
}

admin.form

 <div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">Edit user</h4>
            </div>
            <form action="{{route('user.update','test')}}" method="post">
                {{method_field('patch')}}
                {{csrf_field()}}
                <div class="modal-body">
                    <input type="hidden" name="user_id" id="cat_id" value="">
                    @include('admin.form')
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Save Changes</button>
                </div>
            </form>
        </div>
    </div>
</div>

我在其他一些表上使用了相同的函数技术,有些工作正常,有些却没有。谁能告诉我我在哪里做错了。

2 个答案:

答案 0 :(得分:1)

您应该传递变量     $ user

返回您的视图,以便您可以再次传播它,例如:

<input type="text" class="form-control" name="name" id="name" value={{$user->name}}>

所以你应该做这样的事情

public function update(Request $request){
    $user = User::findOrFail($request->user_id);
    $user->update($request->all());
    return back();
}

到     return view ('admin.form, compact('user'));

或者您也可以使用会话,以便检索数据,然后将其显示在想要显示的刀片上

答案 1 :(得分:0)

https://laracasts.com/discuss/channels/laravel/how-to-pass-id-from-controller-to-route-and-route-to-controller?page=0 Here's the link which you have to read thoroughly and when you click at the edit button, then you have to pass the user id to the modal and you will set query using that particular user id and after that you can display the data in the model, and how you can do it all provided in the link. Also you can do it using java script here's the link. Passing data to a bootstrap modal