代码部分$gallery->photos->first()返回: {"id":3,"gallery_id":1,"filen-6ren">
gpt4 book ai didi

arrays - Laravel - Eloquent - 从返回数组中获取特定值

转载 作者:行者123 更新时间:2023-12-04 10:20:09 24 4
gpt4 key购买 nike

我有以下代码:

     <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()返回:
{"id":3,"gallery_id":1,"filename":"IMG_3700.png","filesize":4229181,"delete":0,"created_at":"2020-03-28T09:56:31.000000Z","updated_at":"2020-03-28T09:56:31.000000Z"}

我需要访问文件名..我尝试了一些事情,但没有一个奏效:
$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 规则生气的原因(在 background-image 规则中没有图像 URL)。它通过一个 php 块预先解决,其中将设置默认(后备)图像 URL:

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

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

关于arrays - Laravel - Eloquent - 从返回数组中获取特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60906469/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com