【问题标题】:join in laravel using "with" & set where statement in joined table使用“with”加入 laravel 并在连接表中设置 where 语句
【发布时间】:2019-01-13 12:28:31
【问题描述】:

我的代码是这样写的:

    $teams = TeamTeam::query()
        ->selectRaw('team_teams.title, team_teams.team_structure_id, team_teams.id,
         team_teams.description')
        ->join('team_structures','team_teams.team_structure_id',"=",'team_structures.id')
        ->where('team_structures.organization_id',session()->get('organization_id'))
        ->orderBy('team_structures.created_at','desc');
    $teams = $teams->get();

我想使用 from with 而不是 join like :

TeamTeam::with('TeamStructure')... 并在 team_structures 表中设置 where 语句。我怎么能用它??!!

谢谢。

【问题讨论】:

  • 我认为我们需要更多的信息。也许是您当前的结果和预期的结果。
  • 我想像 with('TeamStructure') 一样使用“with”加入并设置 where 语句;我目前的结果是正确的。我想知道如何使用该语句编写代码!

标签: laravel join orm eloquent


【解决方案1】:
$teams = TeamTeam::with(['TeamStructure' => function ($q1){
            $q1->where('organization_id', session()->get('organization_id'))->orderBy('created_at', 'desc');
        }])
        ->whereHas('TeamStructure', function ($q2){
            $q2->where('organization_id', session()->get('organization_id'))->orderBy('created_at', 'desc');
        })
        ->select('title', 'team_structure_id', 'id', 'description')
        ->get();

【讨论】:

    猜你喜欢
    • 2017-11-29
    • 2018-05-13
    • 1970-01-01
    • 2017-01-31
    • 2022-10-23
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 2012-10-05
    相关资源
    最近更新 更多