Could you explain me what you are trying to get?
There is few things you need to read to write cleaner code.
Try to use scope for some of the query logic http://laravel.com/docs/5.1/eloquent#query-scopes https://laracasts.com/series/laravel-5-fundamentals/episodes/11
For relational query you can do something like this as well.
$cat = Category::where('slug', 'LIKE', $category)
->with(['shops' => function($query) use ($location){
$query->location($location);
$query->published();
}])->first();
Here we are trying to get shops based on Category and Shop Location and if shop is published.
you can see here I am using the scope here:
$query->location($location);
$query->published();
In my model I have
public function scopePublished($query)
{
return $query->where('status', '=', 'published');
}
public function scopeLocation($query, $location)
{
// Some query logic here.
}
hay @monaye thanks for you kind
I just trying to add whereIn() with below query $query->where('full_name', 'LIKE', '%' . $social_user_search_value . '%')->orWhere('email', 'LIKE', '%' . $social_user_search_value . '%');
but somehow its not filtering with whereIn()
Thanks again :)
I think you're making things too complicated. You can chain the where() statements right from the start and simply save the query object until you call paginate():
$query = SocialUser::orderBy('social_user_id', 'desc');
if (Session::get('USER_TYPE') == '2') {
$location_ids = getPlatformUserLocationIds();
$query->whereIn('location_id', explode(',', $location_ids));
} elseif (Session::get('USER_TYPE') == '3')
$query->whereIn('location_id', explode(',', Session::get('LOCATION_IDS')));
}
if ($social_user_search_value) {
$query->where('full_name', 'LIKE', '%' . $social_user_search_value . '%')->orWhere('email', 'LIKE', '%' . $social_user_search_value . '%');
}
$users = $query->paginate(10);
The order in which you call orderBy(), where() etc doesn't matter since the query will only be build once you call a method that executes it (like get(), first() etc). The way you wrote it is needed if you want to have nested WHERE statements like
WHERE (col1 = "test" OR col2 = "test") AND col3 = "test"
whereIn is the least of your problems. I can see 3 very important issues with this code.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community