仅在存在关联结果时获取对象

仅在存在关联结果时获取对象

本文介绍了仅在存在关联结果时获取对象[laravel]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅当关系查询计数大于0时,我才有获取数据的问题.

I have a problem with get data only when relation query count is more than 0.

这是我的具有关联关系的客户模型

This is my model of customer with relation

class Customer extends Model
{
    protected $table = 'customers';

    public function contracts()
    {
        return $this->hasMany('App\Contract');
    }

这是我的合同

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

最后,我只需要与某天签订合同的客户

On the end i need only customers who they contracts beetwen some date

$customers = Customer::with(['contracts' => function($query)
     {
        $query->where('data_end','>=','2017-07-01')
              ->where('data_end','<=','2017-07-31')
              ->where('typ','=','U') ;
     }
    ])->paginate(10);

但是我有所有顾客.看起来像这样:

But i have all customers. and it looks like this:

"Customer 1"
"Customer 2"
"Customer 3"
  *"Contract 1"
  *"Contract 2"
  *"Contract 3"
"Customer 4"
  *"Contract 1"
  *"Contract 2"
  *"Contract 3"
"Customer 4"

在此示例中,我不需要客户1,2和5.我该如何通过渴望加载和最终带有关系的对象来做到这一点.

In this example i don't need customer 1,2, and 5. How can i do it with eager loading and object with relation on the end.

在此处输入图片描述

发生这种情况,我不需要屏幕截图上带有X的客户-我的意思是,我不需要从哪里查询0个合约的客户

This is happen, i dont need customer with X on the screenshot - I mean, I don't need customer with 0 contracts from where query

-来自dd()的对象-

-- Object from dd()--

此查询结束2个客户,第1个有2个合同,第2个有0个合同

end of this query2 customer, 1st have 2 contracts, 2nd have 0 contracts

LengthAwarePaginator {#217 ▼
  #total: 75000
  #lastPage: 37500
  #items: Collection {#213 ▼
    #items: array:2 [▼
      0 => Customer {#220 ▼
        #table: "customers"
        #connection: null
        #primaryKey: "id"
        #keyType: "int"
        +incrementing: true
        #with: []
        #perPage: 15
        +exists: true
        +wasRecentlyCreated: false
        #attributes: array:5 [▼
          "id" => 1
          "customer_number" => "46071600600"
          "name" => "Nikodem Zalewski"
          "customer_contact" => "507614445"
          "customer_type" => "P"
        ]
        #original: array:5 [▶]
        #casts: []
        #dates: []
        #dateFormat: null
        #appends: []
        #events: []
        #observables: []
        #relations: array:1 [▼
          "contracts" => Collection {#224 ▼
            #items: array:2 [▼
              0 => Contract {#227 ▼
                #connection: null
                #table: null
                #primaryKey: "id"
                #keyType: "int"
                +incrementing: true
                #with: []
                #perPage: 15
                +exists: true
                +wasRecentlyCreated: false
                #attributes: array:10 [▶]
                #original: array:10 [▶]
                #casts: []
                #dates: []
                #dateFormat: null
                #appends: []
                #events: []
                #observables: []
                #relations: []
                #touches: []
                +timestamps: true
                #hidden: []
                #visible: []
                #fillable: []
                #guarded: array:1 [▶]
              }
              1 => Contract {#229 ▶}
            ]
          }
        ]
        #touches: []
        +timestamps: true
        #hidden: []
        #visible: []
        #fillable: []
        #guarded: array:1 [▶]
      }
      1 => Customer {#221 ▼
        #table: "customers"
        #connection: null
        #primaryKey: "id"
        #keyType: "int"
        +incrementing: true
        #with: []
        #perPage: 15
        +exists: true
        +wasRecentlyCreated: false
        #attributes: array:5 [▶]
        #original: array:5 [▼
          "id" => 2
          "customer_number" => "81050371854"
          "name" => "Adam Wróbel"
          "customer_contact" => "560047958"
          "customer_type" => "P"
        ]
        #casts: []
        #dates: []
        #dateFormat: null
        #appends: []
        #events: []
        #observables: []
        #relations: array:1 [▼
          "contracts" => Collection {#222 ▼
            #items: []
          }
        ]
        #touches: []
        +timestamps: true
        #hidden: []
        #visible: []
        #fillable: []
        #guarded: array:1 [▶]
      }
    ]
  }
  #perPage: 2
  #currentPage: 1
  #path: "*"
  #query: []
  #fragment: null
  #pageName: "page"
}

推荐答案

您的代码几乎就在那里.

You are almost there with your code.

您没有指定要使用的Laravel版本,但是您应该查看其中的位置(至少从5.0开始存在于Laravel中)

You don't specify which version of Laravel you're using, but you should be looking at whereHas (This exists in Laravel since at least 5.0)

https://laravel.com/docs/5.0/eloquent#querying-relations (v5.0)

如果您需要更多功能,可以使用whereHas和orWhereHas方法在您的条件查询中放置"where"条件:

If you need even more power, you may use the whereHas and orWhereHas methods to put "where" conditions on your has queries:

尝试以下代码.

$customers = Customer::with('contracts')->whereHas('contracts', function($query)
 {
    $query->where('data_end','>=','2017-07-01')
          ->where('data_end','<=','2017-07-31')
          ->where('typ','=','U') ;
 }
)->paginate(10);

这将向客户加载合同对象,但仅返回具有与查询匹配的合同的客户.

This will load the contracts object with customers but only return customers who have contracts matching the query.

这篇关于仅在存在关联结果时获取对象[laravel]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 01:56