简体   繁体   English

Laravel将数据传递到视图中

[英]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 我写的控制器中的视图称为“ about.blade.php”

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

My view is called "about.blade.php" from my controller I simply wrote return view 我的控制器中的视图称为“ about.blade.php”,我只写了返回视图

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

$full = $first " " . $last;


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

I expected to see the word concatenated text BZ on the page where I wrote {{fullname}} 我希望在我写{{fullname}}的页面上看到串联文本BZ一词。

I get the following error 我收到以下错误

Symfony \\ Component \\ Debug \\ Exception \\ FatalThrowableError (E_PARSE) syntax error, unexpected '" "' (T_CONSTANT_ENCAPSED_STRING) Symfony \\组件\\调试\\异常\\ FatalThrowableError(E_PARSE)语法错误,意外的'“”'(T_CONSTANT_ENCAPSED_STRING)

您还可以使用compact将数据传递到视图中,如下所示:

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

You must define a variable is correct in php $fullname = $first . " " . $last; 您必须在php $fullname = $first . " " . $last;定义一个正确的变量$fullname = $first . " " . $last; $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! 就这样!

I left out the . 我省略了。 for concatenation (: $full = $first . " " . $last; thanks all can close. 对于串联(:$ full = $ first。“”。$ last;谢谢大家可以关闭。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM