0 votes

Pass variable to view laravel from controller

I seek to pass a variable from a controller to a view, but it throws me an error, my controler in the function store redirects to a view where I want to send to call a variable.

My OrdenservicioController

    public function store(Request $request){

    $no_orden=$request->input('no_orden');

    $ordenservicio=new OrdenServicio;
    $ordenservicio->no_orden=$request->get('no_orden');
    $ordenservicio->RFC=$request->get('RFC');
    $ordenservicio->vehiculo=$request->get('vehiculo'); 
    $ordenservicio->mecanico=$request->get('mecanico');

    $ordenservicio->fecha_entrega=$request->get('fecha_entrega');
    $ordenservicio->hora_entrega=$request->get('hora_entrega');
    $ordenservicio->km_recorridos=$request->get('km_recorridos');
    //$ordenservicio->updated_at=$request->get('updated_at'); 
    //$ordenservicio->created_at=$request->get('created_at');
    $date=new \DateTime(); //Obtener fecha del sistema 
    //$clienteveh->fecha=$date->format('Y-m-d');
    $ordenservicio->hora_ingreso=$date->format('H:i:s');
    $ordenservicio->fecha_ingreso=$date->format('Y-m-d');
    $ordenservicio->save(); 
    return redirect('catalogocomponente.index')->with('no_orden',$no_orden);
    //$idorden=OrdenServicio::findOrFail($request->get('RFC')); 
    //return Redirect::to("catalogocomponente",["idorden"=>$idorden]);
}

An input that I want to pull a value from the view OrdendeServicio:

<input type="text" name="no_orden" id="no_orden" class="form-control">  

Here my call of the variable in another view Catalogocomponente.index

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

0voto

Josue Points 1

Added the helper route to your instruction redirect :

return redirect()->route('catalogocomponente.index')->with('no_orden',$no_orden);

With the method with() what these doing is to send a session data to flash, that is why you can't show the data in the view as if it were a variable, for more information I leave you the official documentation of laravel, where it explains in detail all that https://laravel.com/docs/5.8/redirects#redirecting-with-flashed-session-data

To display the value in the view Catalogocomponente.index, you have to add the following code :

@if(session('no_orden'))
    <input type="text" name="no_orden" value="{{session('no_orden')}}" class="form-control">
@endif

0voto

porloscerros Ψ Points 542

If catalogocomponente.index is a view, used return view():

return view('catalogocomponente.index')->with('no_orden',  $no_orden);

If catalogocomponente.index a path, you pass the variable in an array after a comma:

return redirect()->route('catalogocomponente.index', ['no_orden' => $no_orden]);

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X