我是PHP的新手,也是Laravel的新手。我接管了一个旧项目。
我们有这个代码,它工作正常:
public function getList(Request $request)
{
$apiFormat = array();
try
{
$perPage = Input::get('page_size', 10);
$filters = $request->input();
$postRepo = new PostRepo();
$user = $_SESSION["user"];
$listPost = $postRepo->getlist($user->id, $perPage, 0);
$apiFormat['status'] =\Config::get('constants.api.STATUS_OK');
$apiFormat['message'] = \Config::get('constants.api.SUCCESS');
$apiFormat['data'] = $listPost;
} catch (\Exception $ex) {
$apiFormat['status'] =
\Config::get('constants.api.STATUS_ERROR');
$apiFormat['message'] = $ex->getMessage();
}
return response()->json($apiFormat);
}
这会返回10个项目,这些项目会显示在我们的新闻源上。
这一行:
$listPost = $postRepo->getlist($user->id, $perPage, 0);
数据库是否像这样调用:
$sqlCheckHasLike = "SELECT count(*) > 0 FROM `likes` WHERE `post_id` = `posts`.`id` and `user_id` = '".$user_id."'";
$query = DB::table('posts')
->select('posts.*',DB::raw("($sqlCheckHasLike) as is_like"), 'users.full_name', 'users.avatar', DB::raw("post_categories.name as post_categories_name"))
->leftJoin('users', 'posts.user_id', '=', 'users.id')
->leftJoin('post_categories', 'post_categories.id', '=', 'posts.post_category_id')
->where('posts.status',$post_status)
->where('users.status', 1)
->where(function ($query) use ($user_id, $list_user) {
$query->whereIn('user_id', $list_user)
->orwhere('user_id', $user_id);
})
->orderBy('created_at', 'desc')->paginate($pageSize);
return $query;
但是,我们刚刚在新闻源中为每个项目添加了评论。该代码在其他地方处理,但在这里我想找到一个post.id,有多少条评论属于它?这需要包含在回复中。
替换第一种方法,我尝试了这个:
try {
$perPage = Input::get('page_size', 10);
$filters = $request->input();
$postRepo = new PostRepo();
$user = $_SESSION["user"];
$listPost = $postRepo->getlist($user->id, $perPage, 0);
$array_of_rows = [];
foreach ($listPost as $lp) {
$row_as_array = (array) $lp;
$post_id = $lp->id;
$query = "select count(id) as how_many from comments where
post_id = '". $post_id ."'";
$result_array = DB::select($query);
$result_obj = $result_array[0];
$how_many = $result_obj->how_many;
$row_as_array['how_many_comments'] = $how_many;
$array_of_rows[] = $row_as_array;
}
$merged_list_post = (object) $array_of_rows;
file_put_contents("/var/log/api_debugging", "\ncomment count:\n",
FILE_APPEND | LOCK_EX);
file_put_contents("/var/log/api_debugging",
print_r($merged_list_post, true), FILE_APPEND | LOCK_EX);
$apiFormat['status'] = \Config::get('constants.api.STATUS_OK');
$apiFormat['message'] = \Config::get('constants.api.SUCCESS');
$apiFormat['data'] = $merged_list_post;
} catch (\Exception $ex) {
$apiFormat['status']=\Config::get('constants.api.STATUS_ERROR');
$apiFormat['message'] = $ex->getMessage();
}
这不会引发错误(没有异常)但它更改了返回的数据,因此使用此Feed的应用程序不再能够获得所需的内容。
Laravel中是否有官方方法可以轻松地进行多个数据库查询,并在每个内部对象级别组合这些数据?我想避免返回与原始示例中的对象类型不同的对象。
答案 0 :(得分:0)
您可以将原始查询(我刚刚在此处包含前几行)修改为JOIN
comments
表并获取计数:
$query = DB::table('posts')
->select('posts.*',DB::raw("($sqlCheckHasLike) as is_like"), 'users.full_name', 'users.avatar', DB::raw("post_categories.name as post_categories_name"), DB::raw("count(comments.id) as how_many"))
->leftJoin('users', 'posts.user_id', '=', 'users.id')
->leftJoin('post_categories', 'post_categories.id', '=', 'posts.post_category_id')
->leftJoin('comments', 'comments.post_id', '=', 'posts.id')
您还需要添加groupBy
子句:
->groupBy('posts.id')
之前的orderBy
。