【发布时间】:2015-07-07 17:34:51
【问题描述】:
我是 Laravel 的新手,遇到了一个问题。我有 ShopCustomer 表,其中包含 shop_id 和 customer_id。其中有多个 shop_id 条目和不同的 customer_id。它有多对多的关系。
我有一个 shop_id = 2。我想通过他们的名字找到客户 = 'Mat'。我已经用 JOIN 子句尝试了我的代码。我希望将以下查询转换为 Eloquent Query builder 并希望使用 Eager Loading。
$customers = \DB::table('shop_cust')
->join('customers', 'customers.customer_id', '=', 'shop_cust.customer_id')
->select('shop_cust.total_amount', 'customers.*')
->where('customers.name', 'LIKE', 'Mat')
->where('shop_cust.shop_id', '=', '2')
->get();
dd($customers);
这是我尝试过的。
$customers = ShopCust::with(['customer' => function ($query)
use ($customerName) {
$query->where('name', 'LIKE', '%' . $customerName . '%');
}])->where('shop_id','=', $shopId)->get();
【问题讨论】:
-
向我们展示您的尝试。
-
@Kryten 更新问题。
-
我在这里回答过类似的问题stackoverflow.com/questions/29913238/…
-
@kamlesh.bar 嗨,Kamlesh,我最近几天尝试了很多代码。如果你能回答这个问题就太好了。
-
基本上你有两个表权限shopCustomer和Customer吧?
标签: laravel eloquent laravel-5