【问题标题】:How to concatenate string to a $variable in blade?如何将字符串连接到刀片中的 $ 变量?
【发布时间】:2020-10-17 16:45:51
【问题描述】:

我想将我的数据库中的一个变量(在我的例子中称为 $document)添加到我的刀片文件视图中的 URL 资产目录,以使用户能够在网络浏览器。一个例子如下;

// 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> 

【问题讨论】:

    标签: laravel variables laravel-5 laravel-blade laravel-7


    【解决方案1】:

    您使用了错误的运算符。在 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+

    【讨论】:

      【解决方案2】:

      你必须这样使用

      <div class="my-class" style="background: url({{URL::asset('assets/storage/'. $document->file_url)}}) no-repeat center top; background-size: cover;">
      

      【讨论】:

        猜你喜欢
        • 2021-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-03-03
        • 2021-06-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多