表A =库存|表B = ItemAssociation |表C = ItemValue
我有表A,B和C.A和B有一对一的关系,B和C有一对一的关系。我目前正在使用HasManyThrough关系来实现:
public function item(){
return $this->hasManyThrough('App\ItemValue','App\ItemAssociation','id','id');
}
在我的控制器中:
public function orm(){
$inventory = Inventory::getAssocBySteamID(76561198124900864)->get();
$i = 0;
foreach($inventory as $inv){
$this->data[$i] = $inv->item()->get();
$i++;
}
return $this->data;
}
Inventory :: getAssocBySteamID:
public static function getAssocBySteamID($id){
return SELF::where('steamid64','=',$id);
}
这将返回我需要的所有数据,但是,我需要通过表C中的列,即ItemValue模型对此进行排序。
非常感谢任何帮助。
答案 0 :(得分:0)
您可以在getAssocBySteamID函数中添加 - > orderBy(' TableName',' desc')。
或者在 - > get()
对于Eloquent的QB中的Where子句,您也不需要" ="符号。你可以去哪里(' steamid',$ id)
答案 1 :(得分:0)
不要使用静态函数使用范围。尝试使用这样的连接查询:
// Inventory model method
public function scopeAssocBySteamID($query, $id) {
$query->where('steamid64', $id)
->join('ItemValue'. 'ItemValue.id', '=', 'Inventory.ItemValue_id')
->select('Inventory.*')
->orderBy('ItemValue.item_price')
}
然后:
public function orm(){
$inventory = Inventory::assocBySteamID(76561198124900864)->get();
$i = 0;
foreach($inventory as $inv){
$this->data[$i] = $inv->item()->get();
$i++;
}
return $this->data;
}
检查所有表和字段名称,以便测试此示例。