Home > OS >  Passing variable to .blade.php by return on Controller
Passing variable to .blade.php by return on Controller

Time:07-29

I'm trying to pass a varible from the Controller to my .blade.php file. I'm returning the view and compacted variables to the .blade.php but it doens't recognize the variable. This is the code of the Controller.

$contents = Storage::get($request->file('csv_file_1')->store('temporaryfiles'));
$contents2 = Storage::get($request->file('csv_file_2')->store('temporaryfiles'));

return view('comparison.comparison')->with(compact('contents'),$contents)->with(compact('contents2'),$contents2); 

And i'm trying every way just to get an result but instead i'm getting the "Undefined variable $contents" page. The last method i used was a simple

<p>{{$contents}}</p>

I don't think it's correct but i don't really remember how to do it.

CodePudding user response:

In controller return like:

return view('comparison.comparison', compact(['contents', 'contents2']);

And make sure your file is in resources/views/comparison/comparison.blade.php

CodePudding user response:

Try this

$contents = Storage::get($request->file('csv_file_1')->store('temporaryfiles'));
$contents2 = Storage::get($request->file('csv_file_2')->store('temporaryfiles'));

 return view('comparison.comparison', compact('contents','contents2'));

if you have defined a veriable above just use tha name inside compact as above and it can be acced inside blade as <p>{{$contents}}</p>

CodePudding user response:

You can pass variables like that it's not mandatory to use compact.

return view('comparison.comparison', [
    'contents' => $contents,
    'contents2' => $contents2
]);

or if you want with compact:

return view('comparison.comparison')->with(compact('contents', 'contents1'));
  • Related