【问题标题】:Is there a way to select one url among multiple picture urls with "exists" condition in laravel?有没有办法在laravel中具有“存在”条件的多个图片网址中选择一个网址?
【发布时间】:2021-05-24 08:39:38
【问题描述】:

我有几个图片网址,有些可能存在于表格中,有些可能不存在。列是“图片、图片缩略图、公司标识缩略图、公司标识”。我想选择图片缩略图,比如“picture_url”。但是如果“picture_thumbnail”不存在,我想选择“picture”作为“picture_url”。如果“图片”不存在,我想选择 company_logo_thumbnail 等等......结果我将得到一个图片 URL 作为字符串或 null。

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

【问题讨论】:

  • Laravel Jetsream 开箱即用。也许阅读源代码以了解他们是如何做到的。在那之前,请您提供更多关于这个问题的细节,因为它非常模糊。
  • 公共函数 getProfilePhotoUrlAttribute() { return $this->profile_photo_path ? Storage::disk($this->profilePhotoDisk())->url($this->profile_photo_path) : $this->defaultProfilePhotoUrl(); @JustCarty 你的意思是这个?这看起来不像是我的问题的答案。我不知道我怎么能更清楚。请问楼主哪部分不明白

标签: laravel postgresql eloquent


【解决方案1】:

听起来更像是数据库而不是 Laravel(无论如何我能想到的最简单的方法)。

$images = DB::table('images')
    ->select(DB::raw('COALESCE(picture_thumbnail, picture, company_logo_thumbnail, company_logo) as picture_url'))
    ->get();

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

这也是假设您使用 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();

我在选择中使用了company_logos.id,但猜测它可能类似于company_logos.picture 或带有文件名的东西,而不是行的ID。

更新 2

我会将其更改为左连接:

->join('cloudfiles', function ($join) {
        $join->on('userdetails.company_logo_id', '=', 'cloudfiles.id')->where('company_logo_id', '!=', null);
    })
->leftJoin('cloudfiles', function ($join) {
        $join->on('userdetails.company_logo_id', '=', 'cloudfiles.id')->where('company_logo_id', '!=', null);
    })

左连接意味着您仍然可以从userdetails 中获得所有结果,而正常连接意味着只有具有关联cloudfiles 记录的userdetails 会被返回。

其次,我会改变这个:

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

->whereNotNull('company_logo_id')

虽然我认为您可能能够完全消除这种情况。

其他几点,仅供参考:

$boothOwner = Booth::where('id', '=', Request()->booth_id)->firstOrFail();

可能是(除非表中有某种重复的 id)

$boothOwner = Booth::findOrFail(Request()->booth_id);

同样,

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

可能

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

【讨论】:

  • 感谢您的回复。它的工作。另一个问题实际上是我有 company_logo_id 而不是 company_logo。我怎样才能得到它的关系@martincarlin87
  • @baytencere 假设company_logo_idNULL 或一个整数,您可以将其添加到COALESCE 中的列列表中,并且它应该工作相同。如果你想实际检查,你可以添加一个连接,我会更新答案。
  • 好的,但是当我尝试使用连接时,它返回空数组而不是 NULL。它在一开始就打破了 COALESCE 链
  • $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); }) ->select(DB::raw('COALESCE(cloudfiles.thumbnail, cloudfiles.file_url, userdetails.thumbnail, userdetails.picture) as picture_url')) ->get()->pluck('picture_url')[0];
  • 它有效。非常感谢您的回复
猜你喜欢
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
  • 2019-09-16
  • 2019-06-26
  • 1970-01-01
  • 2015-02-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多