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

Parasoul's avatar

Yeah it's better ^^.

For the belongsTo laravel will search for the corresponding hasTo relation in the other model than he take the name of the function and concat it with the primary key. So in your case he was looking for tcodes_id instead of tcode_id.

Maybe i'll do a ticket at laravel to change this to have the same behavior than other relation.

Edit: That's the behavior they wanted according to the documention

"Eloquent determines the default foreign key name by examining the name of the relationship method and suffixing the method name with _id."

Parasoul's avatar

Hey a small correction.

Your relation is a one to many (Though it was a one to one, so i got it why you needed the tcode_id in Ticket and not ticket_id in Tcode). So the relation in your tcode model should be like this :

public function tickets()
{
    return $this->hasMany(Ticket::class);
}
Previous

Please or to participate in this conversation.