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


    $ordenservicio=new OrdenServicio;

    $date=new \DateTime(); //Obtener fecha del sistema 
    return redirect('catalogocomponente.index')->with('no_orden',$no_orden);
    //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">


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 :

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


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