sarav
sarav

Reputation: 238

Laravel 5.2: Is it possible to use where clause condition in eloquent object

Fetched the initial data by joining the other table

$results = Model::join('joining other table')
                ->where('initial condition')
                ->limit(100)->get();

Now, i need to filter the data by some additional condition.

$new = $results->where('column',value); // Additional Condition

I had tried this but it returns empty collection, even though persist in the $results collection. Is it possible to use the where condition in later part ?.

Upvotes: 1

Views: 2112

Answers (2)

Christian Gerdes
Christian Gerdes

Reputation: 298

You can use model relationships, which is very powerful. If you define your relationships between the different models you can call:

User::with(['posts' => function($builder){
  $builder->where('published', true) // this is on the relationship (posts)
}])->get();

You can also do the following:

Post::whereHas(['user' => function($builder){
    $builder->where('confirmed', true);
}])->get();

That will only return the posts where the associated user is confirmed...

Upvotes: 1

UP.
UP.

Reputation: 187

Have you tried: Model::join('joining other table') ->where('initial condition') ->where('column',value) ->limit(100)->get();

Upvotes: 0

Related Questions