【问题标题】:Laravel5 Eloquent QueryLaravel5 雄辩的查询
【发布时间】: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


【解决方案1】:

您所描述的是 Shop 和 Customer 之间的多对多关系。您的 shop_cust 表只是关系的数据透视表。除非您对 shop_cust 有一些特殊功能,否则不需要为其定义模型。

class Shop extends Model {
    public function customers() {
        return $this->belongToMany('Customer', 'shop_cust');
    }
}

class Customer extends Model {
    public function shops() {
        return $this->belongToMany('Shop', 'shop_cust');
    }
}

使用此定义,您的代码将如下所示:

$shop = Shop::find(2);
$customers = $shop->customers()->where('name', 'LIKE', 'Mat');

这里没有真正需要预先加载,因为您只查看一个特定商店的客户。

编辑

如果您想要访问数据透视表上的额外字段,您也可以这样做。首先,您在关系上使用 withPivot() 方法添加对额外字段的访问权限:

class Shop extends Model {
    public function customers() {
        return $this->belongToMany('Customer', 'shop_cust')->withPivot('foo', 'bar');
    }
}

class Customer extends Model {
    public function shops() {
        return $this->belongToMany('Shop', 'shop_cust')->withPivot('foo', 'bar');
    }
}

现在,您可以通过数据透视模型属性访问数据:

foreach ($customers as $customer) {
    echo $customer->pivot->foo;
    echo $customer->pivot->bar;
}

使用数据透视表的文档here

【讨论】:

  • 我正在寻找 ShopCust 模型,因为它包含的数据不仅仅是 shop_id 和 cust_id。
  • @Chopra 我已经更新了我的答案,为这种情况提供了更多信息。由于您现在似乎已经解决了您的问题,您可能不想更改您的代码,但我认为您可能对这些信息感兴趣。
【解决方案2】:

ShopCustomer 模型

class ShopCustomer Eloquent {

    public function customers()
    {
    return $this->hasMany('Customer','customer_id','customer_id');
    }


    public function shops()
    {
    return $this->hasMany('Shop','shop_id','shop_id');
    }

}

客户模型

class Customer Eloquent {

    public function customer_shop()
    {
    return $this->belongToMany('ShopCustomer','customer_id','customer_id');
    }

}

店铺模式

class Shop Eloquent {

        public function shop_customer()
        {
        return $this->belongToMany('ShopCustomer','shop_id','shop_id');
        }

    }

您的查询

$customers = ShopCustomer::
    ->with(arra('customer' => function($q) use ($customerName){
                                        $query->where('name', 'LIKE', '%' . $customerName . '%');
                                    }))
    ->where('shop_id','=', $shopId)->get();

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-01-26
  • 2014-04-12
  • 2021-07-23
  • 1970-01-01
  • 1970-01-01
  • 2017-11-11
  • 2020-01-14
相关资源
最近更新 更多