php - Many belongsTo on the same table Laravel

286

I'm a little disappointed... I'm trying to join my tables with belongsTo but I have a problem.

I have this table : party

Id -- day -- clown -- makeup1 -- makeup2 ...

clown, makeup1, makeup2 are foreign keys, reference to people table.

Peoples table:

Id -- name -- firstname ...

In my system the values of clown, makeup1 and makeup2 are IDs of people table. This is good.

The problem is when I want to show the name of clown, makeup1 and makeup2.

I have the error

Trying to get property of non-object

In my model I defined my relations like this :

public function people()
{
    return $this->belongsTo(People::class, 'clown', 'id');
}
public function makeup1()
{
    return $this->belongsTo(People::class, 'makeup1', 'id');
}
public function makeup2()
{
    return $this->belongsTo(People::class, 'makeup2', 'id');
}

In my view I show the clown like this :

{{ $defaut->people->name }}

It works but the other not.

{{ $defaut-> makeup1->name }}
{{ $defaut-> makeup2->name }}

-> don't work and show

Trying to get property of non-object

I don't understand....

PS : My request in controller is

$defaults = Default::with('people')->with('makeup1')->with('makeup2')->get();

And this one works, I see the relations in var_dump().

<pre>Collection {#654 ▼
#items: array:1 [▼
0 => Default {#619 ▼
  #table: "defaults"
  #fillable: array:8 [▶]
  #connection: "mysql"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:11 [▶]
  #original: array:11 [▶]
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:3 [▼
    "people" => People {#655 ▶}
    "makeup1" => People {#687 ▶}
    "makeup2" => People {#719 ▼
      #fillable: array:7 [▶]
      #connection: "mysql"
      #table: null
      #primaryKey: "id"
      #keyType: "int"
      +incrementing: true
      #with: []
      #withCount: []
      #perPage: 15
      +exists: true
      +wasRecentlyCreated: false
      #attributes: array:10 [▶]
      #original: array:10 [▶]
      #changes: []
      #casts: []
      #dates: []
      #dateFormat: null
      #appends: []
      #dispatchesEvents: []
      #observables: []
      #relations: []
      #touches: []
      +timestamps: true
      #hidden: []
      #visible: []
      #guarded: array:1 [▶]
    }
  ]
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}
]
}
</pre>
Thank you for your advices
185

Answer

Solution:

I found the solution ! To show the relation I have to write :

{{ $defaut->makeup1()->first()->name }}

Thank you

People are also looking for solutions to the problem: PHP MYSQLI while loop gets only 1 row

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.