php - how do I pass on large datasets across different route requests - Laravel

940

Question might be unclear. here's the explanation. Let's say I've:

Route file:

Route::get('testing', '[email protected]');
Route::get('testingtwo', '[email protected]');

Controller file:

public function functionOne() {
  $this->data = generateReallyBigArray();
  return redirect('testingtwo');
}
public function funtionTwo() {
  // Here $this->data is lost. obviously 'coz this controller file got reinstantiated for @functionTwo
 return view('someview', ['data' => $this->data]);
}

$this->data is lost the momenttestingtwo is hit. How do I pass this data across different route requests? Or if there're other ways of doing it.

I was thinking of doing this:

public function functionOne() {
  $this->data = 'somedata';
  return $this->functionTwo();
}
public function funtionTwo() {
  // Here $this->data is lost. obviously 'coz this controller file got reinstantiated for @functionTwo
 // even this doesn't work. Exception: Method get does not exist
 return Route::get('testingtwo', function() {
     return view('someview', ['data' => $this->data]);
 });
}
45

Answer

Solution:

use with() to send data through session -

public function functionOne() {
   $this->data = 'somedata';
   return redirect('testingtwo')->with('data', $this->data);
}

Or you could flash() the data for using on next request.

$request->session()->flash('data', $this->data);
929

Answer

Solution:

the best way for that is traits

trait Data{

    public function getData() {
        // .....
    }
}

and in your controllers write

use Data;

you can use traits over controllers or You can access your controller method like this:

app('App\Http\Controllers\controllerName')->getDataFunction();

This will work, but it's bad in terms of code organisation (remember to use the right namespace for your ControllerName)

People are also looking for solutions to the problem: php - Contact form doesn't work with the new jQuery version (3.3.1)

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.