【问题标题】:query convert into laravel format查询转换成laravel格式
【发布时间】:2017-06-20 09:54:10
【问题描述】:

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

【问题讨论】:

  • 你能看到使用 Laravel 调试器使用上述代码生成的查询是什么吗?
  • ->where('profiles.profile_type','=','T') 这未包含在第一个查询中。
  • @ajreal 我已经更新了我的答案。但这不是问题..
  • ->orderBy('count', 'desc') 应该是 ->orderBy('cass', 'desc') 吗? count 不是一列。你应该从 Laravel 获得完整的查询。
  • 如果你的raw sql工作正常,那么直接在laravel中执行raw sql

标签: php mysql laravel


【解决方案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);

【讨论】:

  • 谢谢...它的工作.. 但我不知道为什么 laravel 格式查询不能从数据库中检索数据
猜你喜欢
  • 1970-01-01
  • 2015-12-13
  • 2019-03-09
  • 2021-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多