how to pass data to the controller from laravel view

0

I have a view in blade , with a table, that through a method, loads me with a foreach all the data of the database, which are requests, those requests have to be approved (changing the field of the database accepted, to 1) or denied, which would eliminate the request without more, my problem comes in that I am not able to pass the necessary parameters to the controller to update the request and disappear, I have tried different ways and I've seen many posts, but none of them exactly matches what I'm trying to do, here's the code:

Vista:

 @if($vacations)
                  <table class="table table-striped table-bordered scrolling-dataTable">
                    <thead>
                      <tr>
                       <th>Nombre</th>
                       <th>Tipo</th>
                       <th>Fecha inicio</th>
                       <th>Fecha fin</th>
                       <th>Observaciones</th>
                       <th>Pendiente</th>
                       <th>Aprobar</th>
                      </tr>
                    </thead>
                    <tbody>
                    @foreach ($vacations as $vacation)
                    <tr>
                      <td>{{ $vacation->name }}</td>
                      <td>{{ $vacation->type }}</td>
                      <td>{{ $vacation->date_from }}</td>
                      <td>{{ $vacation->date_to }}</td>
                      <td>{{ $vacation->observations }}</td>
                      <td>{{ $vacation->aceptado }}</td>

                        <td>  <!-- Single button definir boton para aprobar vacaciones -->
                        <div role="group" class="btn-group">
                          <button id="btnGroupDrop1" type="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" class="btn btn-outline-primary dropdown-toggle dropdown-menu-right"><i class="icon-cog3 icon-left"></i> Settings</button>
                          <div aria-labelledby="btnGroupDrop1" class="dropdown-menu">
                            <a href="{{ url ('/vacation/aceptado',[$vacation->id]) }}" class="dropdown-item">Aprobar</a>
                            <a href="{{url('/vacation/deny')}}" class="dropdown-item">Denegar</a></div>
                          </div>
                         </div>
                       </td>
                   </tr>
               @endforeach

Driver:

 public function update(Request $request, Vacation $vacations)
{
     $this->validate($request, [
        'aceptado' => 'required',
    ]);
    //$inputs=Input::all();
    //$vacation = new Vacation();
    $vacation=Vacation::findOrFail($id);
    $vacation-> aceptado = '1';
    //$vacation-> id = $inputs['id'];

    $vacation->save();

    Mail::send("correo.solicitud", $data, function($message) use ($data){
        $message->to('email','user')
        ->subject("Respuesta a su solicitud");
    });

    return redirect('/vacation/calendar');

}

and the route:

 Route::post('/vacation/update', 'VacationController@update')->name('aceptado');

When I give the button to accept the request, it returns:

  

NotFoundHttpException in RouteCollection.php line 161:

    
asked by Peisou 30.01.2018 в 13:53
source

2 answers

1

The error is because you can not find a route with that URL , as you are defining the route with name at the end, you can use it but you need to pass the parameter $id to the route, When doing click in the link, this is sent by GET , and not by POST , you should change the verb from the route.

Route::get('/vacation/update/{id}', 'VacationController@update')->name('aceptado');

In your view you would call this route, with the helper route passing the parameter, path name and $id

<a href="{{ route('aceptado',$vacation->id)}}"
         class="dropdown-item">Aprobar</a>

And in your controller you should receive this Id as the second parameter

public function update(Request $request,$id) {...}
    
answered by 30.01.2018 / 14:29
source
1

The correct method for the update should be PUT

Route::put('/vacation/{id}', 'VacationController@update')->name('aceptado');

In Blade, the form must have both the CRSF and the correct verb PUT

{!! Form::open(array('url' => ['/vacation/update'], 'method' => 'PUT', 'class' =>'form-horizontal')) !!}

{{ csrf_field() }} 

And in the controller

public function update(Request $request,$id) {...}
    
answered by 31.01.2018 в 13:23