【问题标题】:How to use leftJoin in Laravel for case?如何在 Laravel 中使用 leftJoin 进行案例?
【发布时间】:2018-02-04 16:35:33
【问题描述】:

我有以下代码:

$registrations = User::where(function ($query) use ($request) {
   if (!is_null($request->region) && $request->region !== '0') {
      $query->where('region', $request->region);
   }
})->get();

如何将User 结果与另一个模型ClientsLeft Join 条件连接?

我试过这样:

$registrations = User->join('clients', 'clients.id', '=', 'user.id')->where(...

【问题讨论】:

  • 用户是一个类而不是一个对象,因此你不能使用 -> 所以你应该使用 :: 来调用它的函数
  • 提问前阅读文档

标签: php laravel laravel-5


【解决方案1】:

先阅读文档:https://laravel.com/docs/5.5/queries#joins

$registrations = User::where(....)
    ->join('clients', 'clients.id', '=', 'user.id')
    ->get();

【讨论】:

    【解决方案2】:

    您可以通过阅读docs 来了解它

    $users = DB::table('users')
    ->leftJoin('clients', 'users.id', '=', 'clients.user_id')
    ->where('','')
    ->get();
    

    【讨论】:

      猜你喜欢
      • 2020-12-29
      • 2016-08-27
      • 2014-12-29
      • 2021-05-30
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多