How to send data using redirect with Laravel
Asked Answered
M

5

45

I developed an API to show a pop up message when the page loaded.

Not all the pages use the pop up API. For example, if the user go to the show($id) page, that doesn't required the pop up api to fire. but in some special cases, I need the pop up to be fired.

Here is my code, ** this code is just to illustrate my point, not an actual working code**

public function store(){
    validating the input

    saving them

    $id = get the id

    return Redirect::route('clients.show, $id')
}

and in the show function I do this:

public function show($id){
   $client =Client::find($id)
   return View::make('clients.profife')->with(array(
   'data' => $client
))

My question

Is there a way so I can send a data from the store function to the show function using Redirect::route ? and then in the show function, I check if this show function, I check if this data has been sent of what and then I decide whether to fire the pop up api or not.

}

Memorial answered 1/8, 2014 at 10:41 Comment(0)
H
80

In store()

return Redirect::route('clients.show, $id')->with( ['data' => $data] );

and in show() read it with

Session::get('data');
Havens answered 1/8, 2014 at 10:47 Comment(1)
Take care that with refresh, the data will disappear because with will be pulled one time.Torpedoman
P
44

A simple redirect using helper functions.

So you don't need to set use Redirect nor use Session in your Controller.

After you're done processing something in your Controller, insert:

return redirect()->route('clients.show')->with([ 'id' => $id ]);

To retrieve the variable 'id' in route 'clients.show', use:

// in PHP
$id = session()->get('id');

// in Blade
{{ session()->get('id') }}
Poucher answered 12/9, 2017 at 8:1 Comment(5)
This works fine, and looks a little neater to my eyes.Aldo
@ChuckLeButt Sorry, I've improved my explanation. and thanksPoucher
I was giving you a compliment and gave you a +1 :)Aldo
or just session('id'); (laravel 5.7)Inshore
I like this answer better than the accepted oneKinnikinnick
D
1

After sending the information via the following code in Laravel 8:

return redirect()->route( 'clients.show' )->with( [ 'id' => $id ] );

I will receive the amount sent with $id

Dewie answered 30/11, 2021 at 9:51 Comment(0)
L
0

After storing data printing that stored data via redirect method.

return redirect('/users'.'/'.$id.'/print');
Lanham answered 25/4 at 16:0 Comment(0)
S
-1

Yes you can define path in route like below:

Route::get('store/{id?}/{param1?}/{param2?}/{param3?}', 'clients@store');

You can pass multiple parameter in function and read in controller by passing argument..

You can define in controller :

public function store(id, param1,param2,param3){
//read the param values
}
Spelunker answered 1/8, 2014 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.