【问题标题】:Laravel eloquent relational queries with modelsLaravel 雄辩的关系查询与模型
【发布时间】:2019-06-27 18:31:58
【问题描述】:

在修改搜索表单中的查询代码之前,我使用了下面的查询,效果很好。

...
$query = Job::where('is_trash', 0);
$query = $query->where('created_at', '>=', Carbon::parse($rFrom)->startOfDay())
        ->where('created_at', '<=', Carbon::parse($rTo)->endOfDay())
        ->where('customer_name', 'like', '%' . $rName . '%')
        ->where('project_name', 'like', '%' . $rProject . '%')
        ->where('job_type', 'like', '%' . $rType . '%');
...

但现在我修改如下表结构以动态显示字段名称 通过使用关系查询。

工作表

  +----------------------------------------------------+
     ID | customer_id | job_project_id | job_type_id | ...                                         
  +----------------------------------------------------+
     1  | 1           | 1             | 1       | ...
  +----------------------------------------------------+
     2  | 2           | 7             | 2       | ...
  +----------------------------------------------------+

工作项目表

  +-------------------------------+
     ID | customer_id | name | ...                                         
  +-------------------------------+
     1  | 1           | 1    |...
  +-------------------------------+
     2  | 2           | 7    | ...
  +-------------------------------+

职位类型表

  +------------------+
     ID | name  | ...                                         
  +------------------+
     1  | test1 |...
  +------------------+
     2  | test2 | ...
  +------------------+

Job 模型中还包括如下简单的关系。这些东西将 varchar 类型定义为字符串。但是Job模型在job表中只定义了这些模型的id为customer_id、project_id和jobType_id。

...

public function user()
{
    return $this->hasOne(User::class);
}

public function customer()
{
    return $this->hasOne(Customer::class);
}

public function jobProject()
{
    return $this->hasOne(JobProject::class);
}

public function jobType()
{
    return $this->hasOne(JobType::class);
}
...

主要问题是如果搜索输入是customer_name、project_name和job_type,那么如何建立查询关系查询链呢?因为 Job 表只包含这些 id。

【问题讨论】:

    标签: laravel eloquent laravel-query-builder


    【解决方案1】:
    <?php 
    
    // Build primitive query
    $jobQuery = Job::where('is_trash', 0);
    
    // Use when conditional to check if you need to filter something
    $jobQuery->when($from && $to, function($q) use($request) {
            $q->where('created_at', '>=', Carbon::parse($from)->startOfDay())
                ->where('created_at', '<=', Carbon::parse($to)->endOfDay());
        });
    // Use whereHas to add a where clause in relations
    $jobQuery->when($customerName, function($q) use($request) {
        $q->whereHas('customer', function ($subq) {
            $subq->where('name', 'like', '%' . $customerName . '%');
        });
    });
    
    
    $jobQuery->when($projectName, function($q) use($request) {
        $q->whereHas('jobproject', function ($subq) {
            $subq->where('name', 'like', '%' . $projectName . '%');
        });
    });
    
    $jobQuery->when($jobType, function($q) use($request) {
        $q->whereHas('jobtype', function ($subq) {
            $subq->where('name', 'like', '%' . $jobType . '%');
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2021-06-17
      • 2016-07-01
      • 2019-02-12
      • 1970-01-01
      • 1970-01-01
      • 2017-05-24
      • 2013-06-04
      • 2016-11-26
      • 2016-02-26
      相关资源
      最近更新 更多