vendredi 31 mars 2017

Laravel pass ID back from controller to view via redirect with errors

How can I pass $id back from controller to the same view when update is failed and error is trigerred.

I have view which prints all items from database and also adds edit button to each of the items which triggers modal popup window.

@for ($i =0; $i < count($inventory); $i++)

                     <tr>

                       <td>  </td>
                        <td>
                        <button type="button" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
                          Edit
                        </button>

After editting all the data are passed to route via form

<form action="/updateInventory/" method="post">

Then it goes to my controller which validates input and then inserts input into database.

public function update(Request $req,$id)

{
    $this->validate($req, [
        'name'=> 'min:2'
        ]);
    $inventory = inventory::find($id);
    $inventory->name = $req->input('name');
    $response = $inventory->save();


    if($response)
    {
        return redirect()->back()->with(['message'=>'gerai']);
    }


    return redirect()->back()->withErrors(['error'=>'negerai']);
    //return redirect('/inventory');

}

But if input doesn't pass validation I'm printing error like that.

@include ('partials.notice')

                    @if($errors->any())

                    <script>
                        $(function() {
                            $('#myModal').modal('show');
                        });
                        </script>
                    @endif

How can I pass $id of element I just edited from controller back to the same view so when @if($errors->any()) triggers I can popup my modal $('#myModal').modal('show'); with element I wanted to edit.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2ooM18n
via IFTTT

Aucun commentaire:

Enregistrer un commentaire