Bart
Bart

Reputation: 77

Laravel passing data into view

Not able to pass data to my view from controller.

My view is called "about.blade.php" from my controller I simply wrote

return view('pages.about')->with('fullname', "test");

My view is called "about.blade.php" from my controller I simply wrote return view

$first = 'B';
$last = "Z";

$full = $first " " . $last;


return view('pages.about')->with('fullname', $full);

I expected to see the word concatenated text B Z on the page where I wrote {{fullname}}

I get the following error

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_PARSE) syntax error, unexpected '" "' (T_CONSTANT_ENCAPSED_STRING)

Upvotes: 0

Views: 78

Answers (3)

Giang D.MAI
Giang D.MAI

Reputation: 244

You must define a variable is correct in php $fullname = $first . " " . $last; If you pass variables from controller to view, you have some method below:

return view('pages.about')->with('fullname', $fullname);

return view('pages.about', compact('fullname'));

$data['fullname'] = $fullname; 
return view('pages.about', $data);

That's all!

Upvotes: 2

Ngorld
Ngorld

Reputation: 856

You also use compact to pass data into view like this :

return view('pages.about',compact('var_1', 'var_2', 'var_3'));

Upvotes: 2

Bart
Bart

Reputation: 77

I left out the . for concatenation (: $full = $first . " " . $last; thanks all can close.

Upvotes: 1

Related Questions