0

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)

1
  • use like this $full = $first. " " . $last; Commented Jul 6, 2019 at 5:23

3 Answers 3

2

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

return view('pages.about',compact('var_1', 'var_2', 'var_3'));
Sign up to request clarification or add additional context in comments.

Comments

2

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!

Comments

1

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

2 Comments

Nice one. BTW - this is a perfect use case for accessors - laravel.com/docs/5.8/eloquent-mutators#accessors-and-mutators. See the getFullNameAttribute example. You define it once in your model and then use it throughout your code.
thank you will have a look into that (: & thank you Ivan.

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.