如何通过 ID 作为查询中的主键加入 table -laravel

How to do join table by ID as primary key in query -laravel

我想做一个基于 2 个数据库 tables 的过滤器的功能。但是,我不确定如何将连接 table 放入查询中。这意味着在将结果返回给数据table之前,数据将从两个table(用户和员工table)中筛选出来。

我的过滤器查询是

public function filterQuery(Request $request){

    $age = $request->age;
    $gender= $request->gender;

    $query = user::query();

    if(!empty($request->age)){
        $query->where('age','>=',$age );
    }

    if(!empty($request->gender)){
        $query->where('gender','<=',$gender);
    }

    $data = $query->get();

    return datatables()->of($data)->make(true);
}

我想在查询中加入的 table 来自 table 员工(列 = 收入和 house_ownership)。以及连接两者的主键 tables 是集成电路。

试试这个

public function filterQuery(Request $request){

    $age = $request->age;
    $gender= $request->gender;
    if(!empty($request->age)){
       $data = User::where('age','>=',$age)->get();
    }

    if(!empty($request->gender)){
      $data =  User::where('gender','<=',$gender)->get();
    }


    return datatables()->of($data)->make(true);
}

如果你在两个模型中都有关系,你可以使用whereHas:

    if(!empty($request->income) || !empty($request->house_ownership)){
        $query->whereHas('employee', function($q) use ($income, house_ownership) {
             if (!empty($income)) {
                  $q->where('income', $income);
             }
             if (!empty($house_ownership)) {
                  $q->where('house_ownership', $house_ownership);
             }             
        });
    }
...

或者您可以只使用 join 或 leftjoin 来过滤另一个 table:

public function filterQuery(Request $request){

    $age = $request->age;
    $gender= $request->gender;
    $house_ownership = $request->house_ownership;
    $income= $request->income;

    $query = user::query();
    $query->leftjoin('employee', 'employee.IC', '=', 'user.IC');

    if(!empty($request->age)){
        $query->where('user.age','>=',$age );
    }

    if(!empty($request->gender)){
        $query->where('user.gender','<=',$gender);
    }

    if(!empty($request->income)){
        $query->where('employee.income', $income);
    }
    if(!empty($request->house_ownership)){
        $query->where('employee.house_ownership', $house_ownership);
    }
    $data = $query->select('user.*')->get();

    return datatables()->of($data)->make(true);
}

您可以使用 Eloquent::when() 来减少条件查询的 if-else。

public function filterQuery(Request $request){

    $query = user::query();
    $query->select('user.*')->join('employee', 'employee.IC', '=', 'user.IC');

    $data = $query
        ->when(request('age') != null , function ($q) {
            return $query->where('user.age','>=',request('age'));
        })
        ->when(request('gender') != null , function ($q) {
            return $query->where('user.gender','<=',request('gender'));
        })
        ->when(request('income') != null , function ($q) {
            return $query->where('employee.income','<=',request('income'));
        })
        ->when(request('house_ownership') != null , function ($q) {
            return $query->where('employee.house_ownership','<=',request('house_ownership'));
        })
    ->get();

    return datatables()->of($data)->make(true);
}