【发布时间】:2021-05-27 18:16:02
【问题描述】:
我的项目中有 3 个模型,我想得到一个查询或收集结果,正如我所说:
-
JewelsItem 型号:
protected $table = 'jewel_items'; public function jewel(){ return $this->belongsTo('App\Models\Jewel'); } public function sellInvoice(){ return $this->belongsTo(SellInvoice::class,'sell_invoice_id'); }
2.宝石模型:
public function jewelsItems(){
return $this->hasMany('App\Models\JewelsItem');
}
3.sellInvoice 模型:
protected $table = "sell_invoices";
public function jewelsItems(){
return $this->hasMany(JewelsItem::class,'buy_invoice_id');
}
查询:我想获取所有没有 sellInvoice 且珠宝名称类似于“某物”的珠宝网站。
注意: 珠宝模型有一个名称属性。我想将珠宝的名称添加到结果集合的所有项目的第一个。
我知道如何获得所有名称为“某物”的珠宝:
Jewel::where('name','like','something'.'%')->get();
但我无法获取与其相关的所有jewelItem,并将jewel 的名称添加到其中的第一个。
【问题讨论】:
标签: laravel eloquent laravel-query-builder