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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40749148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 14:47:46  来源:igfitidea点击:

Eloquent - how to add a join in the hasMany relationship?

laraveleloquent

提问by daninthemix

I have a typical hasMany method:

我有一个典型的 hasMany 方法:

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

But how do I add a join and some other conditions to this relationship when I use the ->with('earmarks')to retrieve them? I want to add:

但是,当我使用->with('earmarks')检索它们时,如何向这种关系添加连接和其他一些条件?我想补充:

->join('locations', 'locations.id', '=', 'earmarks.location')
->select('earmarks.*', 'locations.location AS em_location')
->orderBy('date', 'asc');

采纳答案by daninthemix

OK, figured out I need a closure in my with()clause, like this:

好的,发现我的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');

回答by do01

this worked for me

这对我有用

public function earmarks() {
   return $this->hasMany('App\Earmark', 'labtop_id', 'id')
        ->join('locations', 'locations.id', '=', 'earmarks.location')
        ->select('earmarks.*', 'locations.location AS em_location')
        ->orderBy('date', 'asc')->get();
}

it can be a attribute in Labtop Model and be serializable in Labtop json object like this :

它可以是 Labtop 模型中的一个属性,并且可以像这样在 Labtop json 对象中序列化:

public function GetEarmarksAttribute() {
   return $this->hasMany('App\Earmark', 'labtop_id', 'id')
        ->join('locations', 'locations.id', '=', 'earmarks.location')
        ->select('earmarks.*', 'locations.location AS em_location')
        ->orderBy('date', 'asc')->get();
}

protected $appends = array('earmarks');

回答by Alex Lam

When you defined a hasMany method in the Location model (e.g. \App\Models\Location.php)

当您在 Location 模型(例如\App\Models\Location.php)中定义了 hasMany 方法时

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

You can return a Location object in Controller (e.g. LocationController.php):

你可以在 Controller 中返回一个 Location 对象(例如LocationController.php):

public function show($id)
{
    $location = \App\Models\Location::find($id);
    return view('locations.show)
    ->with('location', $location);
}

Then in the view (e.g. locations\show.blade.php), you can retrieve the data like this:

然后在视图中(例如location\show.blade.php),您可以像这样检索数据:

@foreach ($location->earmarks->sortBy('date') as $earmark )
    <p>{{ $earmark->attribute_name }}</p>
@endforeach