查询转换为laravel格式

时间:2017-06-20 09:54:11

标签: php mysql laravel

我需要将此查询转换为laravel格式

select `likes`.`profile_id` as `id`, `assets`.`processed` as `processed`,
       `assets`.`id` as `asst_id`,
       `assets`.`profile_id` as `profile_id`,
       `assets`.`name` as `assets_name`,
       `assets`.`asset_type` as `asset_type`,
       COUNT(assets_id) as cass from likes
left join `assets` on `assets`.`id` = `likes`.`assets_id`
left join `assets_data` on `assets_data`.`asset_id` = `assets`.`id` 
left join `profiles` on `assets`.`profile_id` = `profiles`.`id`
where `assets`.`asset_type` = 'V'
and `assets`.`access` = 'PUB'
group by `likes`.`assets_id`
order by `cass` desc

我正在尝试此查询,但它不检索数据,但上面的查询工作正常

$dataaa=  DB::table('likes')->select('likes.profile_id as id','profiles.name as pro_name','assets_data.thumb_img as thumb_img','assets.processed as processed','assets.id as asst_id','assets.profile_id as profile_id','assets.name as assets_name','assets.asset_type as asset_type','assets_data.path as assets_path', DB::raw('COUNT(likes.assets_id) as cass'))
              ->leftJoin('assets', 'assets.id', '=', 'likes.assets_id')
              ->leftJoin('assets_data', 'assets_data.asset_id', '=', 'assets.id')
              ->leftJoin('profiles', 'assets.profile_id', '=', 'profiles.id')
              ->where("assets.asset_type",'=',$like_type)
              ->where (`assets`.`asset_type`, '=', 'V')
              ->where ('assets.access','=','PUB')
            ->groupBy('likes.assets_id')
            ->orderBy('cass', 'desc')
            ->take($take)
            ->get();

当我调试查询时。 laravel生成此查询

        select `likes`.`profile_id` as `id`, `profiles`.`name` as `pro_name`, `assets_data`.`thumb_img` as `thumb_img`, `assets`.`processed` as `processed`, `assets`.`id` as `asst_id`, `assets`.`profile_id` as `profile_id`, `assets`.`name` as `assets_name`, `assets`.`asset_type` as `asset_type`, `assets_data`.`path` as `assets_path`,
     COUNT(likes.assets_id) as cass from `likes`
     left join `assets` on `assets`.`id` = `likes`.`assets_id`
     left join `assets_data` on `assets_data`.`asset_id` = `assets`.`id`
     left join `profiles` on `assets`.`profile_id` = `profiles`.`id`
 where `assets`.`asset_type` = V
 and `assets`.`access` = PUB
 group by `likes`.`assets_id`
 order by `cass` desc limit 8

1 个答案:

答案 0 :(得分:1)

如果你的原始sql工作正常

,直接执行raw sql
$sql = "select `likes`.`profile_id` as `id`, `assets`.`processed` as `processed`,
       `assets`.`id` as `asst_id`,
       `assets`.`profile_id` as `profile_id`,
       `assets`.`name` as `assets_name`,
       `assets`.`asset_type` as `asset_type`,
       COUNT(assets_id) as cass from likes
left join `assets` on `assets`.`id` = `likes`.`assets_id`
left join `assets_data` on `assets_data`.`asset_id` = `assets`.`id` 
left join `profiles` on `assets`.`profile_id` = `profiles`.`id`
where `assets`.`asset_type` = 'V'
and `assets`.`access` = 'PUB'
group by `likes`.`assets_id`
order by `cass` desc";

$result = DB::select($sql);