When I query $user = User::find(1), I'm getting the phone model with $user->phone, but I can't get the phone id with $user->phone->id, it's always returning 0.
What's wrong with getting the id, what should I do to getit ?
It's possible that the id column in the phones table is not set as the primary key. In that case, you can specify the foreign key and local key in the relationship definition. For example:
class User extends Model
{
public function phone()
{
return $this->hasOne(Phone::class, 'user_id', 'id');
}
}
Here, user_id is the foreign key in the phones table that references the id column in the users table.
If this is not the issue, it's possible that the id column in the phones table is not being populated correctly. Check that the id column has a default value of NULL and that it is set to auto-increment.
@jaseofspades88
Thanks for your answer, it seems to be due to my primary key, not the relationship:
I probably have to rewrite my migration with standard auto-increment integer id and the token in a token column instead of id.