سلام استاد
من اومدم قبل از اینکه از scopeها استفاده کنم
کل لاجیک رو در کنترلر categoryVideoController قرار دادم
<?php namespace App\\Http\\Controllers; use App\\Models\\Category; use Illuminate\\Http\\Request; use Illuminate\\Support\\Facades\\DB; class CategoryVideoController extends Controller { public function index(Request $request, Category $category) { $videos = $category->videos(); if ($request->has('sortBy') && $request->sortBy == 'created_at') { $videos = $videos->orderBy('created_at', 'desc'); } if ($request->has('sortBy') && $request->sortBy == 'like') { $videos = $videos->leftJoin('likes', function ($join) { $join->on('likes.likeable_id', '=', 'videos.id') ->where('likes.likeable_type', '=', 'App\\\\Models\\\\Video') ->where('likes.vote'); })->groupBy('videos.id')->select(['videos.*', DB::raw('count(likes.vote) as vote')])->orderBy('vote', 'desc'); } if ($request->has('sortBy') && $request->sortBy == 'length') { $videos = $videos->orderBy('length', 'desc'); } if ($request->has('length') && $request->length == 1) { $videos = $videos->where('length', '<', 60); } if ($request->has('length') && $request->length == 2) { $videos = $videos->whereBetween('length', [60, 300]); } if ($request->has('length') && $request->length == 3) { $videos = $videos->where('length', '>', 300); } $videos = $videos->paginate(18); $name = $category->name; return view('videos.index', compact('videos', 'name')); } }
و برای sort براساس لایک به خطای زیر میخورم
SQLSTATE[42000]: Syntax error or access violation: 1055 'video_share3.videos.name' isn't in GROUP BY
چدین بار بررسی کردم کوئری سمت لایکها رو ولی مشکلی ندیدم
از طرفی کل اطلاعات videos میخوام و نمیشه بیخیال videos.name شد
dd هم ک میگیرم کوئریم مشکلی نداره
if ($request->has('sortBy') && $request->sortBy == 'like') { $videos = $videos->leftJoin('likes', function ($join) { $join->on('likes.likeable_id', '=', 'videos.id') ->where('likes.likeable_type', '=', 'App\\\\Models\\\\Video') ->where('likes.vote','=',1); })->groupBy('videos.id')->select(['videos.*', DB::raw('count(likes.vote) as vote')])->orderBy('vote', 'desc'); dd($videos->dd()); }
result
"select `videos`.*, count(likes.vote) as vote from `videos` left join `likes` on `likes`.`likeable_id` = `videos`.`id` and `likes`.`likeable_type` = ? and `likes`.`vote` = ? where `videos`.`category_id` = ? and `videos`.`category_id` is not null group by `videos`.`id` order by `vote` desc ◀ array:3 [▼ // vendor\\laravel\\framework\\src\\Illuminate\\Database\\Query\\Builder.php:3754 0 => "App\\Models\\Video" 1 => 1 2 => 1 ]