Questions tagged «many-to-many»

类型A和类型B的实体之间的一种关系,将类型B的实体列表与类型A的实体相关联,反之亦然。类型A和B可以是相同类型。

3
获取额外的数据透视表列laravel的值
我有一个phone_models,phone_problems和一个phone_model_phone_problem数据透视表。数据透视表有一个额外的“价格”列。 手机型号: class PhoneModel extends \Eloquent { public function problems() { return $this->belongsToMany('RL\Phones\Entities\PhoneProblem')->withPivot('price'); } } 电话问题: class PhoneProblem extends \Eloquent { public function models() { return $this->belongsToMany('PhoneModel')->withPivot('price'); } } 我想做的是获取具有特定问题的特定手机的价格。 这就是我现在的方式,但是我觉得Laravel具有内置的Eloquent功能,我找不到用更简单的方式做到这一点: $model = $this->phoneService->getModelFromSlug($model_slug); $problem = $this->phoneService->getProblemFromSlug($problem_slug); 这一切都是从他们的中选择特定的模型和问题。 然后我要做的就是凭这些凭证获得价格,如下所示: $row = DB::table('phone_model_phone_problem') ->where('phone_model_id', '=', $model->id) ->where('phone_problem', '=', $problem->id) ->first(); 所以现在我可以得到这样的价格,$row->price但是我觉得需要一种更简单,更“ …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.