【问题标题】:selecting certain columns from parent model with Eloquent laravel使用 Eloquent laravel 从父模型中选择某些列
【发布时间】:2017-01-05 10:54:48
【问题描述】:

我为Content的名称制作模式

class Content extends Model
{
    /**
     * The attributes that aren't mass assignable.
     *
     * @var array
     */
    protected $guarded = [];

    /**
     * Get Images
     *
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function images()
    {
        return $this->hasMany(Image::class, 'content_id')->orderBy('sort');
    }
}

我仍然在 controller

中使用以下代码
$data = \App\Models\Content::has('images')
                                   ->leftJoin('content_relations AS r', 'contents.id', '=', 'r.content_id')
                                   ->leftJoin('products AS p', 'contents.id', '=', 'p.content_id')
                                   ->whereIn('r.category_id', [1, 2, 3])
                                   ->groupBy('contents.id')->take(4)->get();

当我打电话时,它会为我提供图像记录

foreach ($data as $row) {
    var_dump($row->images);
}

如果我更改了 controller 中的代码以从 Content Model 中获取某些列,则只需添加选择功能

$data = \App\Models\Content::has('images')->select('title', 'cover', 'slug')
                                           ->leftJoin('content_relations AS r', 'contents.id', '=', 'r.content_id')
                                           ->leftJoin('products AS p', 'contents.id', '=', 'p.content_id')
                                           ->whereIn('r.category_id', [1, 2, 3])
                                           ->groupBy('contents.id')->take(4)->get();

那么它就不返回数据了

foreach ($data as $row) {
    var_dump($row->images);
}

【问题讨论】:

    标签: php model laravel-5.2 relationship


    【解决方案1】:

    您正在调用没有 id 的 $data 上的图像关系

    您在内容类中的图像功能将需要 content_id。 所以取数据的时候要选择id,

    试试这个

    $data = \App\Models\Content::has('images')->select('content.id','title', 'cover', 'slug')
                                           ->leftJoin('content_relations AS r', 'contents.id', '=', 'r.content_id')
                                           ->leftJoin('products AS p', 'contents.id', '=', 'p.content_id')
                                           ->whereIn('r.category_id', [1, 2, 3])
                                           ->groupBy('contents.id')->take(4)->get();
    

    【讨论】:

    • 很高兴帮助 imran。请接受此答案,以帮助解决此类问题的其他人
    【解决方案2】:

    您必须在内容行中包含id,因为我想这就是图像与您的内容模型相关联的方式。如果不包含此内容,则无法将内容模型链接到这些图像。

    另外,考虑使用eager loading,这将预先加载所有图像并使用更少的查询。

    $data = \App\Models\Content::has('images')
        ->with('images')
        ->select('id', 'title', 'cover', 'slug')
        ->leftJoin('content_relations AS r', 'contents.id', '=', 'r.content_id')
        ->leftJoin('products AS p', 'contents.id', '=', 'p.content_id')
        ->whereIn('r.category_id', [1, 2, 3])
        ->groupBy('contents.id')->take(4)->get();
    

    您还应该为产品创建一个关系,这将使您的代码更加简洁,并允许在此处进行预加载。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-10
      • 2019-06-15
      • 1970-01-01
      • 2015-09-28
      • 1970-01-01
      • 1970-01-01
      • 2017-11-29
      • 2021-12-24
      相关资源
      最近更新 更多