我在 Laravel 中有一个元素,我想在 index.blade 中显示一篇来 self 的数据库的文章
所以在 Controller 中我得到了文章并将其保存在一个变量中 问题是:那篇文章的图像不在 index.blade 中,它在公共(public)目录的 css 中,所以我如何用我从数据库中获得的图像更改它?在我的 css 文件中,您可以看到该图像作为 url
Controller
public function home(Request $request)
{
$headerIntroduction=Category::where('name','معرفی کسب و کار')->get()->first()->articles()->get()->values()[1];
return view('app.index',compact('headerIntroduction'));
CSS
#slider-section, #home-section{
background-image:url(../../images/app/slider/1.jpg);
background-repeat:no-repeat;
background-size:cover;
background-color:#282f1f;
position:relative;
color:#fff;
padding: 130px 0;
最佳答案
只有在谈到 View 部分时,您才能在 Laravel 的 blade 模板中使用变量。所以在 css 文件中使用这个变量是不可能的。
如果你还想用的话。然后,您必须在 <style>css</style>
中使用样式标签( index.blade.php
,通常称为内联 css)中需要变量的样式。文件(或任何 Blade 文件)。
我假设图像 1.jpg 文件来自您的 article
那你为什么不使用下面的代码。
<style>
.backgroundclass
{
background-image:url(../../images/app/slider/{{your-article-variable}});
background-repeat:no-repeat;
background-size:cover;
background-color:#282f1f;
position:relative;
color:#fff;
padding: 130px 0;
}
</style>
关于php - 如何在 laravel 中公开访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56335192/