gpt4 book ai didi

laravel - Eloquent -如何在hasMany关系中添加联接?

转载 作者:行者123 更新时间:2023-12-04 18:59:39 25 4
gpt4 key购买 nike

我有一个典型的hasMany方法:

public function earmarks() {
return $this->hasMany('App\Earmark');
}

但是,当我使用 ->with('earmarks')检索它们时,如何为该关系添加联接和一些其他条件?我要添加:
->join('locations', 'locations.id', '=', 'earmarks.location')
->select('earmarks.*', 'locations.location AS em_location')
->orderBy('date', 'asc');

最佳答案

OK,发现我需要在with()子句中关闭,如下所示:

    $updated_laptops = Laptop::with([
'earmarks' => function($q) {
$q
->join('locations', 'locations.id', '=', 'earmarks.location')
->select('earmarks.*', 'locations.location AS em_location')
->orderBy('date', 'asc');
}
])->addJoins()->selectListCols()->find($request->IDs)->keyBy('id');

关于laravel - Eloquent -如何在hasMany关系中添加联接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40749148/

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