Support the ongoing development of Laravel.io →
Architecture Artisan Blade
Last updated 1 year ago.
0
Solution

below is the updated code that you can use to get the active courses.

public function posts(Request $request) {

			$query = $request->input('query');

            return Post::select (
				"posts.title",
				"posts.description",
				"posts.body",
				"categories.name as category_name",
				"post_statuses.status as post_status",
		    )
			->leftJoin("categories", "posts.category_id", "=", "categories.id")
			->leftJoin("post_statuses", "posts.status", "=", "post_statuses.id")
			// filter by status
			->where('post_statuses.status', '=', 'active')
            ->where(function($Query) use($query){
                return $Query->where('title', 'like', '%' . $query . '%')
			                  ->orWhere('description', 'like', '%' . $query . '%')
			                  ->orWhere('body', 'like', '%' . $query . '%')
			                  ->orWhere('categories.name', 'like', '%' . $query . '%')
			                  ->orWhere('post_statuses.status', 'like', '%' . $query . '%');
             })
			
			->orderBy('posts.id','DESC')
			->paginate(10);
	}
0
Solution selected by @ajax30

Problem: The problem is you were doing the OR operation of status and $query value. Solution: You have to perform the AND operation between the status and the $query value. The closure takes care of OR operation for searching purposes.

0

@faisal Pleas help me with this question too. Thanks!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Razvan ajax30 Joined 2 Oct 2021

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.