【问题标题】:Laravel one to one relationships and queryLaravel 一对一关系和查询
【发布时间】:2019-03-23 14:00:08
【问题描述】:

我有客户被归类为(个人-公司),我想从主表和子表中获取所有客户信息到一个查询中,我尝试了很多次,但我没有找到解决方案..我的尝试结束本主题

我的桌子:

客户类型

  • id
  • type

客户

  • id
  • address
  • customer_type_id

customer_individual

  • id
  • first_name
  • last_name
  • customer_id

客户公司

  • id
  • company_name
  • logo
  • customer_id

我的关系:

CustomerType.php

class CustomerType extends Model
{
    public function customers()
    {
        return $this->hasMany('App\Customer');
    }
}

Customer.php

class Customer extends Model
{
    public function customer_type()
    {
        return $this->belongsTo('App\CustomerType');
    }

    public function more()
    {
        $related = ($this->customer_type_id == '1') ? 
        'App\CustomerIndividual' : 'App\CustomerCompany'; 
             // 1 => individual

        return $this->hasOne($related, 'customer_id');
    }
}

CustomerIndividual.php

class CustomerIndividual extends Model
{
    public function customer()
    {
        return $this->belongsTo('App\Customer');
    }
}

CustomerCompany.php

class CustomerCompany extends Model
{
    public function customer()
    {
        return $this->belongsTo('App\Customer');
    }
}

我希望输出如下:

[
  {
     "id": 1,
     "customer_type_id": 1,
     "address": "New York",
     "more": {
        "id": 1,
        "customer_id": 1,
        "first_name": "John",
        "last_name": "doe"
     }
  },
  {
     "id": 2,
     "customer_type_id": 2,
     "address": "london",
     "more": {
        "id": 2,
        "customer_id": 2,
        "name": "ANA IT Company",
        "logo": "analogo.png"
     }
  }
]


这些是我的尝试:

return Customer::with(['person', 'company'])->get();

$customers = Customer::query();

$customers->when($customers->customer_type_id === 1, function($q){
   return $q->with('person');
});

$customers->when($customers->customer_type_id === 2, function($q){
   return $q->with('company');
});

$c = $customers->get();
return $c;

希望能在这里找到解决办法。

【问题讨论】:

    标签: laravel relationship one-to-one


    【解决方案1】:

    您可以做的是创建一个新集合并将它们添加到其中。

    假设$q->with('person')是一个对象:

    $collection = new \Illuminate\Database\Eloquent\Collection;
    
    $customers->when($customers->customer_type_id === 1, function($q){
        $collection->push($q->with('person'));
    });
    
    $customers->when($customers->customer_type_id === 2, function($q){
        $collection->push($q->with('company')); 
    });
    
    $c = $collection->flatten();
    return $c;
    

    文档:Here

    【讨论】:

    • 未定义变量 $collection 更多... (⌘F1) "未定义属性:Illuminate\Database\Eloquent\Builder::$customer_type_id"
    • 但这也将 $customer_type_id 称为 undefiend?
    • 我的代码:$customers = Customer::query(); $collection = new \Illuminate\Database\Eloquent\Collection; $customers->when($customers->customer_type->id === 1, function($q){ $collection->push($q->with('person')); }); $customers->when($customers->customer_type->id === 2, function($q){ $collection->push($q->with('company')); }); $c = $collection->flatten(); return $c;
    猜你喜欢
    • 1970-01-01
    • 2018-09-27
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 2014-08-22
    • 2018-06-16
    相关资源
    最近更新 更多