Arrays Laravel-雄辩-从返回数组中获取特定值

Arrays Laravel-雄辩-从返回数组中获取特定值,arrays,laravel,eloquent,Arrays,Laravel,Eloquent,我有以下代码: <div class="col-8 offset-2 mb-5 p-3 rounded-lg shadow d-flex justify-content-between

我有以下代码:

     <div class="col-8
                    offset-2
                    mb-5
                    p-3
                    rounded-lg
                    shadow
                    d-flex
                    justify-content-between
                    align-items-baseline"
             style="background-color: lightgrey;
                 background-image: linear-gradient(to right, rgba(211, 211, 211, 0.52), rgba(211, 211, 211, 0.73)), url('/images/social_{{$gallery->photos->first()}}');  ">
我需要访问文件名。。我尝试了一些方法,但没有一种有效:

$gallery->photos->first('filename')
$gallery->photos->first()->get('filename')
$gallery->photos->first()['filename']
$gallery->photos->first()->filename
上次尝试(
$gallery->photos->first()->filename
)返回此错误:

Facade\Ignition\Exceptions\ViewException Trying to get property 'filename' of non-object (View: C:\xampp\htdocs\galshare\resources\views\home.blade.php)
如何访问返回的文件名


谢谢

在讨论了这个问题之后,我们得出结论,
$gallery
有时返回一个空集,这是CSS规则生气的原因(在
背景图像
规则中没有图像URL)。它是通过一个php块预先解决的,其中将设置默认(回退)图像URL:

@php
$filename = '/path/to/default.jpg';
if ($gallery->isNotEmpty() && $gallery->photos->isNotEmpty()) {
    $filename = $gallery->photos->first()->filename;
}
@endphp
<style>
...
</style>
@php
$filename='/path/to/default.jpg';
如果($gallery->isNotEmpty()&&&$gallery->photos->isNotEmpty()){
$filename=$gallery->photos->first()->filename;
}
@endphp
...

这段代码也可以在控制器中完成,从那里可以发送
$filename
(即)变量,变量的值为
$gallery->photos->first()->filename | |'/path/to/fallback.jpg'

返回什么
$gallery->photos->first()->filename
?试图获取非对象的属性“filename”(视图:C:\xampp\htdocs\galshare\resources\views\home.blade.php)时,这应该可以工作。这必须工作:
$filename=null;如果($gallery&&!empty($gallery->photos)){$photo=$gallery->photos->first();$filename=$photo->filename;}
对于显示的代码的第一行,这必须起作用。@Tpojka谢谢,但是我如何调整它以在这里输入它呢<代码>…社交/社交{{{$gallery->photos->first()}}')请重写您的问题并包含所有相关信息。你现在问的问题在上面的问题中没有显示出来。强烈建议您阅读本文,并相应地重写您的问题。
@php
$filename = '/path/to/default.jpg';
if ($gallery->isNotEmpty() && $gallery->photos->isNotEmpty()) {
    $filename = $gallery->photos->first()->filename;
}
@endphp
<style>
...
</style>