Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

query condition on "with" relation not work #2265

Unanswered
sepisoltani asked this question in Q&A
Discussion options

I have a Order Model which has belongsTo relation to Factor model

use Jenssegers\Mongodb\Eloquent\Model;
class Order extends Model
{
 public function factor()
 {
 return $this->belongsTo(Factor::class, 'factor_id');
 }
}

I want to load orders which only their factor status is 'payed'.
I run this query but it loads all orders.

 $res = Order::with(['factor' => function ($query) {
 $query->where('status', '=',"payed");
 }])->get()

if I change the "with" , to "whereHas" it works.
why "with" doesn't work ?

You must be logged in to vote

Replies: 1 comment

Comment options

With-> only relations records will be filtered. WhereHas will filter main table's record.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
Converted from issue

This discussion was converted from issue #2227 on May 28, 2021 09:42.

AltStyle によって変換されたページ (->オリジナル) /