@@ -74,17 +74,16 @@ public function search($searchQuery, $categoryId, $levelId, $sort)
7474 'rating ' ,
7575 ])
7676 ->with (['category:id,name ' , 'level:id,slug,name ' , 'instructor:id,name,avatar_url ' ])
77- ->when ($ searchQuery !== '' , function ($ q ) use ($ searchQuery ) {
78- $ term = "% {$ searchQuery }% " ;
79- $ q ->where (function ($ q ) use ($ term ) {
77+ ->when (filled ($ searchQuery ), function ($ q ) use ($ searchQuery ) {
78+ $ q ->where (function ($ q ) use ($ searchQuery ) {
8079 $ q
81- ->where ('title ' , 'like ' , $ term )
82- ->orWhere ('short_description ' , 'like ' , $ term )
83- ->orWhere ('description ' , 'like ' , $ term );
80+ ->where ('title ' , 'LIKE ' , " % { $ searchQuery } % " )
81+ ->orWhere ('short_description ' , 'LIKE ' , " % { $ searchQuery } % " )
82+ ->orWhere ('description ' , 'LIKE ' , " % { $ searchQuery } % " );
8483 });
8584 })
86- ->when ($ categoryId !== '' , fn ($ q ) => $ q ->where ('category_id ' , (int ) $ categoryId ))
87- ->when ($ levelId !== '' , fn ($ q ) => $ q ->where ('level_id ' , (int ) $ levelId ))
85+ ->when (filled ( $ categoryId) , fn ($ q ) => $ q ->where ('category_id ' , (int ) $ categoryId ))
86+ ->when (filled ( $ levelId), fn ($ q ) => $ q ->where ('level_id ' , (int ) $ levelId ))
8887 ->when ($ sort === 'newest ' , fn ($ q ) => $ q ->latest ())
8988 ->when ($ sort === 'rating ' , fn ($ q ) => $ q ->orderByDesc ('rating ' )->orderByDesc ('id ' ))
9089 ->paginate (4 );
0 commit comments