【发布时间】:2019-04-20 23:48:56
【问题描述】:
我在基于 hasMany 关系从数据库中获取数据并将其发送到 Larave 中的 api 时遇到问题。 在下面的代码中,Laravel 没有给我 Customer id = $id 的 Customer 产品。我不知道为什么,我无法在 $query 旁边发送 $id。如果我在 '' 中写数字,我有很好的答案,但我需要自动接受。
$products = Product::where('name', 'like', '%'.$request->q.'%')->orWhere('symbol', 'like', '%'.$request->q.'%')->with(['customers' => function ($query, $id) {
$query->where('id', '=', $id);
}])->get();
所有api代码
Route::get('/customer/{id}/products', function(Request $request, $id){
$customer = Customer::findOrFail($id);
$products = Product::where('name', 'like', '%'.$request->q.'%')->orWhere('symbol', 'like', '%'.$request->q.'%')->with(['customers' => function ($query, $id) {
$query->where('id', '=', $id);
}])->get();
return $products;
});
提前谢谢你。
【问题讨论】: