2017-06-20 60 views
-2

我需要这个查询转换成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 
+0

可以看到的是查询采用上面Laravel调试器的代码生成了什么? – Webinion

+0

' - > where('profiles.profile_type','=','T')'这不包括在第一个查询中。 – ajreal

+0

@ajreal我有更新我的答案。但这不是问题。 –

回答

1

直接执行原始的SQL如果你的原始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); 
+0

谢谢...其工作..但我不不知道为什么laravel格式查询不从数据库中检索数据 –