0
我在我的应用程序中有三个数据库表。作为一个例子,这里是我现在有:在Laravel中有两个或多个外键的表格
items TABLE: id, quantity, price, month_id (FK), department_id (FK)
months TABLE: id, month
department TABLE: id, department
我已经定义的模型hasMany
和belongsTo
关系。通过这些关系,我可以执行查询App\Month::first()->items
来检索所有第一个月的项目。但是,如果我想查询月份和部门,在这种情况下最好的方法是什么?
我当然可以就这样做:
$month = App\Month::first();
$month->items()->where('department_id', '3')->get();
是否有这样做的查询更优雅的方式?
试试这个:' month :: with(['items'=> function($ q){$ q-> where('department_id',3)}]) - > first();' –