如何将字符串连接到 blade 中的 $variable?
How to concatenate string to a $variable in blade?
我想将一个变量(在我的例子中称为 $document)从我的数据库添加到 URL 资产目录中 blade 文件使用户能够在 Web 浏览器中查看图像。举例如下;
// MyController File
// the variable $code is a parameter I'm accepting from the web.php route file for
// the function show() to help process the assignment value of $data['document'] without issues
public function show($code)
{
// NB: I am passing the document as an array list item of $data as below, so I can mention it as
// a variable in the blade view file
// $data['document']
$data['document'] = Auth::user()->documents->where("code", $code)->first();
return view("documents.show", $data);
}
// Blade View File
<div class="my-class" style="background: url({{ URL::asset('assets/storage/' +$document->file_url) }}) no-repeat center top; background-size: cover;">
</div>
您使用了错误的运算符。在JavaScript中,连接运算符是+,但在PHP中,它是.。要使上面的代码正常工作,您只需将其更新为如下所示。
// Blade View File
<div class="my-class" style="background: url({{URL::asset('assets/storage/' . $document->file_url)}}) no-repeat center top; background-size: cover;">
</div>
注意:使用的测试环境是Laravel 7+
你必须这样使用
<div class="my-class" style="background: url({{URL::asset('assets/storage/'. $document->file_url)}}) no-repeat center top; background-size: cover;">
我想将一个变量(在我的例子中称为 $document)从我的数据库添加到 URL 资产目录中 blade 文件使用户能够在 Web 浏览器中查看图像。举例如下;
// MyController File
// the variable $code is a parameter I'm accepting from the web.php route file for
// the function show() to help process the assignment value of $data['document'] without issues
public function show($code)
{
// NB: I am passing the document as an array list item of $data as below, so I can mention it as
// a variable in the blade view file
// $data['document']
$data['document'] = Auth::user()->documents->where("code", $code)->first();
return view("documents.show", $data);
}
// Blade View File
<div class="my-class" style="background: url({{ URL::asset('assets/storage/' +$document->file_url) }}) no-repeat center top; background-size: cover;">
</div>
您使用了错误的运算符。在JavaScript中,连接运算符是+,但在PHP中,它是.。要使上面的代码正常工作,您只需将其更新为如下所示。
// Blade View File
<div class="my-class" style="background: url({{URL::asset('assets/storage/' . $document->file_url)}}) no-repeat center top; background-size: cover;">
</div>
注意:使用的测试环境是Laravel 7+
你必须这样使用
<div class="my-class" style="background: url({{URL::asset('assets/storage/'. $document->file_url)}}) no-repeat center top; background-size: cover;">