为了账号安全,请及时绑定邮箱和手机立即绑定

将 Symfony 查询转换为 Laravel 6.x Eloquent 查询

将 Symfony 查询转换为 Laravel 6.x Eloquent 查询

PHP
一只斗牛犬 2022-06-11 10:06:59
我目前正在将 Symfony 3.4 站点迁移到 Laravel 6.x,并且我正在寻找将此查询转换为 Laravel Eloquent 查询的正确方法。这是 Symfony 查询 $qb = $this->createQueryBuilder('up'); $qb->select('COUNT(up.id)'); $qb->leftJoin('up.workout', 'w')        ->leftJoin('up.user', 'u')        ->where('up.parent IS NULL AND up.deleted = false')        ->andWhere('up.status != \'Just started\' OR up.status is NULL')        ->andWhere('w.workoutType = \'Normal\' AND u.deleted = false')        // Long Cardio, Long Cardio (Burn) and Long Cardio (Hardcore)        ->andWhere('up.completed = true OR (up.completed_cardio_shred = true AND ((w.level = \'Long Cardio\') OR (w.level = \'Long Cardio (Burn)\') OR (w.level = \'Long Cardio (Hardcore)\')))');  $qb->getQuery()->getSingleScalarResult();这是我使用 Laravel 6.x DB Query 生成的 $userProgressTable = DB::table('user_progress');    $userProgressTable->select('id');    $userProgressTable->leftJoin('fos_user', 'fos_user.id', '=', 'user_progress.user_id');    $userProgressTable->leftJoin('workout', 'workout.id', '=', 'user_progress.workout_id');    $userProgressTable->whereRaw('user_progress.parent_id IS NULL');    $userProgressTable->where('user_progress.deleted', '=', false);    $userProgressTable->whereRaw('user_progress status <> \'Just started\' OR user_progress.status IS NULL');    $userProgressTable->where('workout.workout_type', '=', 'Normal');    $userProgressTable->where('user.deleted', '=', false);    $userProgressTable->whereRaw('user_progress.completed = true OR (user_progress.completed_cardio_shred = true AND ((workout.level = \'Long Cardio\') OR (workout.level = \'Long Cardio (Burn)\') OR (workout.level = \'Long Cardio (Hardcore)\'))');    $userProgressTable->where('user_progress.user_id', '=', $user->id);    return $userProgressTable->count();我的问题是,如果我使用 Eloquent 模型/查询方式,上面的查询会是什么样子?
查看完整描述

1 回答

?
牛魔王的故事

TA贡献1830条经验 获得超3个赞

首先,您必须拥有模型类本身,并正确设置任何关系。从您共享的代码中,我看到了三个不同的模型。通常,您希望每张桌子都有一个模型,但这也有点依赖于桌子。例如,您不需要一个用于数据透视表(但您可以拥有一个)。


class UserProgress extends Eloquent

{

    ...

}


class FosUser extends Eloquent

{

    ...

}


class workout extends Eloquent

{

    ...

}

您可以为类命名任何您喜欢的名称,它们不必与表名匹配(有关详细信息,请参阅文档:https ://laravel.com/docs/5.8/eloquent#eloquent-model-conventions )。


然后将您的关系添加到需要它们的每个模型(https://laravel.com/docs/5.8/eloquent-relationships),然后您就可以开始查询了。它可能看起来像这样:


$result = UserProgress::join('fos_user', 'fos_user.id', '=', 'user_progress.user_id')

    ->join('workout', 'workout.id', '=', 'user_progress.workout_id')

    ->whereRaw('...')

    ...

    ->where('user_progress.user_id', '=', $user->id)

    ->get(); // this returns a Collection object


return $result->count(); // This is a Collection method

这只是一个示例,您可以通过多种方式构建 where 子句(where('user_id', '=', $user.id)vswhereUserId($user.id)等),但应该足以让您入门。


此外,对于这个特定的查询,您实际上只需要第一个模型,但最终您可能最终会使用其他两个表的模型。


查看完整回答
反对 回复 2022-06-11
  • 1 回答
  • 0 关注
  • 113 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号