Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 2 years ago.
0

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 http://laravel.com/docs/5.1/eloquent#query-scopes https://laracasts.com/series/laravel-5-fundamentals/episodes/11

  2. 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. 

    }

0

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 9 years ago.
0

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"
0

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 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

mizan3008 mizan3008 Joined 30 Apr 2015

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.