Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 是否有一种方法可以在多个图片url中选择一个url,并使用;“存在”;拉维尔的情况?_Laravel_Postgresql_Eloquent - Fatal编程技术网

Laravel 是否有一种方法可以在多个图片url中选择一个url,并使用;“存在”;拉维尔的情况?

Laravel 是否有一种方法可以在多个图片url中选择一个url,并使用;“存在”;拉维尔的情况?,laravel,postgresql,eloquent,Laravel,Postgresql,Eloquent,我有几个图片URL,其中一些可能存在于表中,一些可能不存在。列为“图片、图片、图钉、公司徽标、公司徽标”。我想选择图片缩略图,比如说“图片url”。但如果“图片缩略图”不存在,我想选择“图片”作为“图片url”。如果“图片”不存在,我想选择公司标志和缩略图等等。。。因此,我将得到一个图片url作为字符串或null 编辑:这应该在单个查询中完成,而不是使用if-else进行多个查询。或者更优雅的解决方案也会受到欢迎。听起来更像是数据库,而不是Laravel(这是我能想到的最简单的方法) MySQL

我有几个图片URL,其中一些可能存在于表中,一些可能不存在。列为“图片、图片、图钉、公司徽标、公司徽标”。我想选择图片缩略图,比如说“图片url”。但如果“图片缩略图”不存在,我想选择“图片”作为“图片url”。如果“图片”不存在,我想选择公司标志和缩略图等等。。。因此,我将得到一个图片url作为字符串或null


编辑:这应该在单个查询中完成,而不是使用if-else进行多个查询。或者更优雅的解决方案也会受到欢迎。

听起来更像是数据库,而不是Laravel(这是我能想到的最简单的方法)

MySQL函数返回第一个非空结果

这也是假设您使用MySQL作为数据库引擎

更新

要检查另一个表中的列,可以使用联接

$images = DB::table('images')
    ->select(DB::raw('COALESCE(picture_thumbnail, picture, company_logo_thumbnail, company_logo, company_logos.id) as picture_url'))
    ->leftJoin('company_logos', 'company_logos.id', '=', 'images.company_logo_id')
    ->get();
我在select中使用了
company\u logos.id
,但我猜它可能类似于
company\u logos.picture
或带有文件名的内容,而不是行的id

更新2

我会将此更改为左连接:

->join('cloudfiles', function ($join) {
        $join->on('userdetails.company_logo_id', '=', 'cloudfiles.id')->where('company_logo_id', '!=', null);
    })
左连接意味着您仍然可以从
userdetails
中获得所有结果,而正常连接意味着只返回具有关联
cloudfiles
记录的
userdetails

其次,我要改变这一点:

->where('company_logo_id', '!=', null)

虽然我认为你可以完全消除这种情况

其他几点,仅供参考:

$boothOwner = Booth::where('id', '=', Request()->booth_id)->firstOrFail();
可能是(除非表中有重复的ID)

同样地

->whereRaw('userdetails.user_id = ?', [$boothOwner->user_id])
可能是

->where('userdetails.user_id', $boothOwner->user_id)

这是开箱的。也许阅读源代码,看看他们是如何做到的。在此之前,请您提供一些关于这个问题的详细信息,因为它非常模糊。公共函数getProfilePhotoUrlAttribute(){返回$this->profile\u photo\u path?存储::磁盘($this->profilePhotoDisk())->url($this->profile\u photo\u path):$this->defaultProfilePhotoUrl();}@JustCarty你是说这个?这似乎不是我问题的答案。我不知道怎样才能更清楚。你能问一下你不明白哪一部分吗?谢谢你的回答。它起作用了。另一个问题是,实际上我有公司标识,而不是公司标识。我怎样才能得到它@martincarlin87@baytencere假设
company\u logo\u id
NULL
或一个整数,您可以将其添加到
COALESCE
中的列列表中,并且其工作方式应相同。如果您想实际检查,您可以添加一个联接,我将更新答案。好的,但是当我尝试使用联接时,它将返回空数组而不是NULL。它在$boothOwner=Booth::where('id','=',Request()->Booth_id)->firstOrFail()开头打破了聚合链;return DB::table('userdetails')->whereRaw('userdetails.user_id=?',[$boothOwner->user_id])->join('cloudfiles',function($join){$join->on('userdetails.company_logo_id','=','cloudfiles.id')->where('company_logo_id','!=',null);})->选择(DB::raw('COALESCE'(cloudfiles.缩略图,cloudfiles.file_url,userdetails.缩略图,userdetails.picture)作为picture_url'))->get()->pulk('picture_url')[0];很有效。非常感谢您的回复
$boothOwner = Booth::where('id', '=', Request()->booth_id)->firstOrFail();
$boothOwner = Booth::findOrFail(Request()->booth_id);
->whereRaw('userdetails.user_id = ?', [$boothOwner->user_id])
->where('userdetails.user_id', $boothOwner->user_id)