MyungHun
MyungHun

Reputation: 77

Laravel orm with() not works

JoinSkipReason has not id, but JoinReview's primary key is foreign key of JoinSkipReason.

Query Builder works well...

I want to use with() to join the JoinSkipReason to the JoinReview table.

Thank you.

JoinReview::with(
    'join_skip_reason'
);
class JoinReview extends Model
{
    public function joinSkipReason()
    {
        return $this->hasOne('App\Models\Service\JoinSkipReason');
    }
}
class JoinSkipReason extends Model
{
    protected $fillable = ['join_review_id', 'reason'];

    public function joinReview()
    {
        return $this->belongsTo('App\Models\Service\JoinReview', 'id');
    }
}

Upvotes: 0

Views: 63

Answers (2)

NevNein
NevNein

Reputation: 547

I've encountered this issue myself, the issue is that for eager loading multi-word relations you should use camelCase.

In your code:

JoinReview::with(
  'joinSkipReason'
);

I've also found a (kinda old) issue saying that you should also access the eager-loaded relation as camelCase, otherwise it will ignore the cached result and query the database again.

Upvotes: 1

Lalit Kumar
Lalit Kumar

Reputation: 360

Check do like that

public function joinSkipReason()
{
    return $this->hasOne('App\Models\Service\JoinSkipReason','join_review_id','id');
}

Upvotes: 0

Related Questions