【问题标题】:Select except some columns in laravel eloquent when i use from "with" in join queries当我在连接查询中使用“with”时,选择除了 laravel eloquent 中的某些列
【发布时间】:2019-08-11 07:42:12
【问题描述】:

我在 laravel 的 join eloquent 中使用 from "with",我想在获取查询结果时使用 select,但返回错误,因为例如我使用 with(['user]) 并且 user 是我的模型名称而不是表名称(表的名称是用户) 有什么方法可以在我的模型名称中使用“选择”吗? 或者我使用除了获取除特定列之外的所有默认列?

谢谢

这是我的查询:

AlbumGalleryManagement::with('image','user')
        ->whereHas('organization', function ($q2) use ($organizationSlug){
            $q2->where('organization_slug', $organizationSlug);
        })
        ->join('province_approves','province_approves.approved_id','album_gallery_managements.id')
        ->where('province_approves.approved_type',self::ALBUMMODEL)
        ->where('published_at','<=',$nowDate)
        ->orderBy('published_at','DESC')
        ->orderBy('album_gallery_managements.id','DESC')
        ->paginate(self::PAGINATE);

【问题讨论】:

    标签: join select eloquent


    【解决方案1】:

    我使用 select() 来获取指定的列。 在join中使用“with”;列结果进入关系标签,我很容易使用它们。

    我的最后一个查询:

          AlbumGalleryManagement::with('image')
            ->whereHas('organization', function ($q2) use ($organizationSlug){
                $q2->where('organization_slug', $organizationSlug);
            })
            ->selectRaw('album_gallery_managements.*')
            ->join('province_approves','province_approves.approved_id','album_gallery_managements.id')
            ->where('province_approves.approved_type',self::ALBUMMODEL)
            ->where('province_approves.status_type',config('constants.status_type.approved'))
            ->where('published_at','<=',$nowDate)
            ->orderBy('published_at','DESC')
            ->orderBy('album_gallery_managements.id','DESC')
            ->paginate(self::PAGINATE);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      • 2017-11-29
      • 1970-01-01
      • 2015-08-19
      • 2020-10-15
      • 2020-04-22
      相关资源
      最近更新 更多