【问题标题】:Laravel join no return datas correctlyLaravel 加入没有正确返回数据
【发布时间】:2020-04-16 20:22:03
【问题描述】:

我需要获取状态为“Disponible”的所有 Prestation,并且项目将用户的名字和姓氏关联在三个表之间。 我试过这个:

$prestations = Prestation::whereHas('item', function($query) use ($available) {
           $query->where('status', $available);
        })
        ->join('items', 'items.prestation_id', '=', 'prestations.id')
        ->join('users', 'users.id', '=', 'items.user_id')
        ->select('prestations.*', 
                 'users.first_name as firstname', 
                 'users.last_name as lastname'
                )
        ->get();

    return $prestations;

可以获取项目状态为“Disponible”的所有 Prestation,但我的选择没有返回我想要的数据,我只为每个“Prestation”获得一个“_id”。

感谢您的帮助。

【问题讨论】:

    标签: php laravel eloquent entity-relationship laravel-query-builder


    【解决方案1】:

    item和prestations是什么关系。

    $prestations = DB::table(prestations)
            ->join('items', function (JoinClause $join) use (available) {
                 $join->on('items.prestation_id', '=', 'prestations.id');
                 $join->where('items.status', $available);
            })
            ->join('users', 'users.id', '=', 'items.user_id')
            ->select('prestations.*', 
                     'users.first_name as firstname', 
                     'users.last_name as lastname'
                    )
            ->get();
    
        return $prestations;
    

    我觉得你可以试试。

    【讨论】:

    • 嗨,谢谢你的建议,你的代码给我“是消息:参数 1 传递给 App\Http\Controllers\Conciergerie\PrestationsController::App\Http\Controllers\Conciergerie\{closure}( ) 必须是 JoinClause 的实例,给定的 Illuminate\Database\Query\JoinClause 实例"
    • 哦,让我们添加“使用 Illuminate\Database\Query\JoinClause;”在你的课堂上。
    • 谢谢,我试过了,它的工作原理,但我只得到每个元素的 _id 而不是其余的
    猜你喜欢
    • 2019-06-15
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2015-07-17
    相关资源
    最近更新 更多