Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jake2025's avatar

Laravel's belongsTo relationship

Greetings Pros,

I know this is easy for you guys but i'm really confused about this.

I have implemented a pagination ( which you can found in one of sir jeffrey's video activity feed is the video's name i think ) and etc...

Here's the relationship:

class Activity extends Eloquent {

    //

    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function subject()
    {
        return $this->morphTo()->withTrashed();
    }

}
RecipientNumber Model

    public function withDeletedRecipients() {
        return $this->belongsTo('App\Recipient')->withTrashed();
    }

Now i've deleted some recipient and for some point it displays an error saying:

Trying to get property of non-object (View: C:\wamp\www\textblast\resources\views\events\created_recipientnumber.blade.php) (View: C:\wamp\www\textblast\resources\views\events\created_recipientnumber.blade.php)

but when i include recipient_id in withDeletedRecipients

RecipientNumber Model

    public function withDeletedRecipients() {
        return $this->belongsTo('App\Recipient', 'recipient_id')->withTrashed();
    }

It works perfectly. but if i remove the recipient_id it displays the error above.

Can someone enlighten me?

Thank you in advance.

0 likes
7 replies
jake2025's avatar

Just now i just replaced the recipient_id with id and it still works @_@.

pmall's avatar

With belongs to relationships, the foreign key id is guessed from the relationship name. As your relationship is named withDeletedRecipients, you have to explicitely specify the name of the foreign key, product_id.

jake2025's avatar
public function numbers() {
        return $this->hasMany('App\RecipientNumber');
    }

but this one works without the foreign key id :/.

jake2025's avatar

Oh yea i forgot this one is hasMany sorry. Lol.

jake2025's avatar

@pmall just now i replaced recipient_id with id and it still works @_@. I'm really confused right now. Sorry if i have to ask you directly sir.

JarekTkaczyk's avatar
Level 53

@jake2025 You need the FK like @pmall said already. It will work with id (without any error to be precise), but it won't do what you want it to do. It will be completely wrong results.

Now, the error you're getting is simply caused by not checking whether relation exists (in your view), thus you get trying to get property of non-object error. That said just add some check, like:

@if ($activity->recipient)
  <p>{{ $activity->recipient->name }}</p>
@endif

Also withDeletedRecipients is definitely bad name. What does it tell you $activity->withDeletedRecipients if you're getting single related model here?

jake2025's avatar

Oooooh. I think i got it now.

Yeah sorry for the method name but i already have public function recipient which is the softDeleted recipients should not be included so i need to provide another that calls the recipients including the softdeleted ones..

Please or to participate in this conversation.