【问题标题】:Get object only when relation result is exist [laravel]仅在存在关系结果时获取对象[laravel]
【发布时间】:2017-07-27 15:34:20
【问题描述】:

只有当关系查询计数大于 0 时,我才会获取数据。

这是我的客户模型与关系

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');
    }
}

最后,我只需要在某个日期与他们签约的客户

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

但我有所有客户。它看起来像这样:

"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。我如何通过急切加载和最后的关系对象来做到这一点。

enter image description here

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

-- 来自 dd() 的对象--

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

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"
}

【问题讨论】:

    标签: php laravel orm relation eager


    【解决方案1】:

    你的代码就快到了。

    您没有指定您使用的是哪个版本的 Laravel,但您应该查看 whereHas(这至少从 5.0 开始就存在于 Laravel 中)

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

    访问模型的记录时,您可能希望根据关系的存在来限制结果。例如,您希望提取所有至少有一条评论的博客文章。为此,您可以使用 has 方法:

    如果您需要更多功能,您可以使用 whereHas 和 orWhereHas 方法在您的 has 查询中添加“where”条件:

    试试下面的代码。

    $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);
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-05
      • 2017-03-27
      • 2016-08-03
      • 1970-01-01
      • 2016-12-10
      • 1970-01-01
      • 2021-02-13
      相关资源
      最近更新 更多