5

I'm trying to pass the value of an array from one method to another method in Laravel. Basically I have a method called orderingSubmission that recieves an array from an Ajax POST execution (that part works fine), but then I'm trying to pass the array values to another method called orderingSubmissionReceipt so I can finally render the view on order-summary page. I'm using sessions but they don't seem to work. Can anyone tell me what I'm missing please?

Here's my route.php

Route::get('/', function() { return redirect('home'); });
Route::get('home', 'HomeController@home');
Route::post('home', 'HomeController@activateCustomer');
Route::post('order-summary', 'HomeController@OrderingSubmission');
Route::get('order-summary', 'HomeController@orderingSubmissionReceipt');

HomeController.php

public function orderingSubmission(Request $request)
{
    $allValues2 = $request->get('users'); // This code works fine. It gets the array
    Log::info('This is orderingSubmission.....: ' . print_r($allValues2, true));

   Session::set('allValues2',$allValues2);

}

public function orderingSubmissionReceipt()
{
    $allValues3 = Session::get('allValues2'); // This code is not getting the session value
    Log::info('This is orderingSubmissionReceipt.....: ' . print_r($allValues3, true));

    return view('order-summary', [
        'people' => $allValues3
    ]);
}
0

4 Answers 4

4

You can use $request->session->put() and $request->session->get() as follows : Do not forget to add Request $request in orderingSubmissionReceipt function declaration.

public function orderingSubmission(Request $request)
{
    $allValues2 = $request->get('users'); // This code works fine. It gets the array
    Log::info('This is orderingSubmission.....: ' . print_r($allValues2, true));
    $request->session()->put('allValues2',$allValues2);

}

public function orderingSubmissionReceipt(Request $request)
{

    $allValues3 = $request->session()->get('allValues2'); // This code is not getting the session value
    Log::info('This is orderingSubmissionReceipt.....: ' . print_r($allValues3, true));

    return view('order-summary', [
        'people' => $allValues3
    ]);
}
Sign up to request clarification or add additional context in comments.

Comments

2

You can create class variables, like this:

class A
{
private $allValues2 = array();

    function orderingSubmission(Request $request)
    {
        $this->allValues2 = $request->get('users');

        //the rest of the code in this method..
    }

    function orderingSubmissionReceipt()
    {
        //if you want to make a local variable first, you can do it like this:
        $allValues3 = $this->allValues2;
        Log::info('This is orderingSubmissionReceipt.....: ' . print_r($allValues3, true));

        //or, you can just use the variable directly, like this:
        Log::info('This is orderingSubmissionReceipt.....: ' . print_r($this->allValues2, true));
    }
}

Comments

1

You can use session() helper method in case Session is not working or showing any error.

public function orderingSubmission(Request $request)
{
    $allValues2 = $request->get('users');
    session()->set('allValues2', $allValues2);
    Log::info('Value set to session: ' . session()->get('allValues2')); 
}

public function orderingSubmissionReceipt()
{
    Log::info('Value get from session: ' . session()->get('allValues2'));     
    $allValues3 = session()->get('allValues2');

    return view('order-summary', [
        'people' => $allValues3
    ]);
}  

Comments

-1

You should do something like that:

use Illuminate\Http\Request; //You probably already have that!

public function orderingSubmission(Request $request)
{
    $allValues2 = $request->get('users');
    $request->session()->put('allValues2', $allValues2);
    Log::info('Value set to session: ' . $request->session()->get('allValues2'));
}

public function orderingSubmissionReceipt(Request $request)
{
    Log::info('Value get from session: ' . $request->session()->get('allValues2'));
    $allValues3 = $request->session()->get('allValues2');

    return view('order-summary', [
        'people' => $allValues3
    ]);
}

For more: https://laravel.com/docs/5.1/session

3 Comments

the results are the same.
or maybe I'm doing it wrong. How would you use $request->session() in my code?
Just edited the answer. Check if that will work! I've tried it with simple variables and works well for me. If that doesn't work, try to encode the array as a json before put it in an array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.