Support the ongoing development of →
posted 8 years ago
Last updated 2 years ago.

Could you explain me what you are trying to get?

There is few things you need to read to write cleaner code.

  1. Try to use scope for some of the query logic

  2. For relational query you can do something like this as well.

$cat = Category::where('slug', 'LIKE', $category)
					->with(['shops' => function($query) use ($location){

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:


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 :)

Last updated 8 years ago.

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.

  1. learn to use scopes so you can re-use code
  2. Why is your user type stored in a session?? That seems very dangerous.
  3. What is this? $location_ids = getPlatformUserLocationIds(); is that a global function?
Last updated 8 years ago.

Sign in to participate in this thread!


Your banner here too?

mizan3008 mizan3008 Joined 30 Apr 2015


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

Your logo here?

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

© 2024 - All rights reserved.