Seize result

-1

I would like to take advantage of the result of a query in another function when requested

For example:

Controller

 public function funcao1(Request $request){
       $myObj = DB::select("SELECT * FROM myTable");
       return view('myView');
    }

Then I would like to use this $ myObj object in another function when it comes from my view, through a click or via ajax

Ajax

var form = $('<form action="{{ route('myRoute') }}"> method="post"'+
             '<input type="hidden" name="_token" value="{{ csrf_token }}"'> 
             '</form>')
$('body').append(form);
form.submit();

Controller

In this part, I would like to take advantage of the object I set in the first function

public function funcao2(Request $request){
   echo "<pre>";
   print_r($myObj);
   echo "</pre>";
}

My route would stay

Route::post('/obj', ['as' => 'myRoute', 'uses' => 'MyController@funcao2']) ;
    
asked by anonymous 24.10.2018 / 17:01

1 answer

1

Because I'm using Laravel, I see there's a lot wrong with your code.

But what could be done, responding objectively, would use $this to assign the value to it.

So:

class MeuController extends Controller
{
    public function __construct()
    {
        $this->myObject = DB::table('...');
    }


    public function funcao1()
    {
        return view('xxx', ['myObject' => $this->myObject]);
    }

    public function funcao2()
    {
        print_r($this->myObject);
    }
}
    
24.10.2018 / 17:16