I am encountering what I would assume to be a common issue but I have not found a relevant solution.
In my Laravel application, I have a relationship setup between two models: BarterReview and BarterReviewFeedback.
BarterReview.php
public function barterReviewFeedback () {
return $this->hasOne(BarterReviewFeedback::class, 'barter_review_id');
}
BarterReviewFeedback.php
public function barterReview () {
return $this->belongsTo(BarterReview::class);
}
The Following returns a BarterReview
with the associated BarterReviewFeedback
:
$review = \App\BarterReview::with('barterReviewFeedback')->find(1);
return $review;
The result ($review
) looks like this when formatted:
{
"id": 1,
"created_at": "2020-04-06T20:13:15.000000Z",
"updated_at": "2020-04-06T20:13:15.000000Z",
"rating": 5,
"comment": "Five out of Five!",
"user_id": 2,
"barter_id": 1,
"barter_review_feedback": {
"id": 3,
"created_at": "2020-06-02T16:34:41.000000Z",
"updated_at": "2020-06-02T16:34:41.000000Z",
"barter_review_id": 1,
"user_id": 1,
"content": "test"
}
}
The ISSUE is that I am unable to access "barter_review_feedback" inside the object - $review->barter_review_feedback
doesn't display anything.
Additionally, attempting to directly access a property inside of $review->barter_review_feedback
such as $review->barter_review_feedback->id
yields the following error: Trying to get property 'id' of non-object
Any ideas as to why this would be?
You can access relationship like this
$review->barterReviewFeedback->id
Because your relationship is barterReviewFeedback
not barter_review_feedback