如何将字符串连接到刀片中的$变量?

nii*_*nte 2 variables laravel laravel-5 laravel-blade laravel-7

我想将一个变量(在我的例子中称为$document)从我的数据库添加到刀片文件视图中的 URL 资产目录,以使用户能够在 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> 
Run Code Online (Sandbox Code Playgroud)

nii*_*nte 5

您使用了错误的运营商。在 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> 
Run Code Online (Sandbox Code Playgroud)

注意:使用的测试环境是 Laravel 7+