Laravel 5.5. Recover inciial filter in an index view around the back of a back view

1

I have made a CRUD with the typical views of create, edit, index and show. In the index view I have defined a field to perform a filter that works correctly. What I would like to know is how to maintain the initial filter of the index view when returning, for example, from the edit view once a record has been modified.

Currently what I do in the driver for the edition is:

public function update(bancoRequest $request, $id)
{

    $banco = banco::find($id);
    $banco->fill($request->all());

    if ($banco->isDirty())
    {

        $banco->flag_inactivado = $request->has('flag_inactivado') ? 1 : 0;
        $banco->save();

        flash::success('El banco <b>'.$banco->getCodigoBanco() . ' '. $banco->nombre .'</b> ha sido modificado con éxito.')->important();
    }
    else
    {
         flash::success('Sin cambios que registrar.');
    }

    return redirect()->route('bancos.index');

}

but when redirecting to the index view again all records are shown again and I want to show only the ones initially filtered before clicking the edit button of the updated record.

    
asked by emgodi 21.03.2018 в 21:23
source

1 answer

0

So that you do not lose the value of the filter you could send it to that update method and after receiving it when you call again, you send it to the view:

 //Enviar en tu formulario de actualizacion podria ser mediante un input hidden

//En vista
<form method="POST" action="TuControlador@update" >
    <input type="hidden" name="_token" value="{{ csrf_token() }}"></input>
     <input type="hidden" name="valor_filtro" value="valor_filtro"></input>
      //Demas campos
    <button type="submit">Enviar</button>
</form>
    //En controlador
    public function update(bancoRequest $request, $id)
    {

        $valor_filtro = $request->valor_filtro();//Recibes valor del filtro
        $banco = banco::find($id);
        //$banco->fill($request->all());//Modificar este metodo para enviar los parametros por separado
        $banco->fill([
        'campo_1' => 'valor_campo_1',
        'campo_2' => 'valor_campo_2'
         ]);
         if ($banco->isDirty())
         {

           $banco->flag_inactivado = $request->has('flag_inactivado') ? 1 : 0;
           $banco->save();

           flash::success('El banco <b>'.$banco->getCodigoBanco().''.$banco->nombre .'</b> ha sido modificado con éxito.')->important();
          }
         else
         {
            flash::success('Sin cambios que registrar.');
         }

    //return redirect()->route('bancos.index');
     //Enviar a la vista el valor del filtro
     return view('bancos.index'/*Cambiar bancos.index por el directorio de tu imagen*/, compact('valor_filtro'));

}

Then receive it in the view with blade asking with

@if(isset($valor_filtro))
   //Asignar valor de tu filtro al elemento html
@else
  //Mostrar filtro por defecto sin valor
@endif
    
answered by 22.03.2018 в 14:28