Abdul Rehman
Abdul Rehman

Reputation: 159

Unable to Retrieve Old Data in Edit Form in Laravel

I want to get data of that id when pressed the edit button. My all fields remain empty. I'm using same page for Create & Edit.

Update Function

 public function update(Request $request)
{

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

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

    return back();
}

Edit Button

 <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>

admin.form

<div class="form-group">
                <label for="name">Name</label>
                <input type="text" class="form-control" name="name" id="name">
            </div>

            <div class="form-group">
                <label for="email">Email</label>
                <input type="text" class="form-control" name="email" id="email">
            </div>

I m using same function techniques on some other tables, some are working fine some are not. Can anyone please tell me where I'm doing wrong in this.

Upvotes: 0

Views: 1157

Answers (2)

user6940918
user6940918

Reputation:

you should pass your variable $user

back to your view so you can propagate it again like:

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

so you should do something like this

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

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

or you may also use sessions so you could retrieve the data then display it to the blade you desire to display it

Upvotes: 1

Malik Awaisee
Malik Awaisee

Reputation: 34

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

Upvotes: 0

Related Questions