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

cooperino's avatar

Eloquent uses wrong foreign key with belongsTo even after dump-autoload

I was getting an error:

(PDOException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'phones.users_id'

Then I realized that the Users model name is in plural, so I edited the relationship:

public function user()
    {
        return $this->belongsTo(Users::class,'user_id');
    }

and ran composer dump-autoload and even php artisan cache:clear but I still get the same error.

Why is it not working?

*Edit: Looks like the Observer code is not updating from some reason

0 likes
13 replies
Snapey's avatar

change your model name to singular

try and stick with conventions, you will experience far less issues

1 like
Snapey's avatar

this seems off

public function phone()
    {
        return $this->belongsTo(Users::class,'user_id');
    }

if this code is in the Phone model then i expect it to be called user()

if in the User model then it would be phones() and be a hasMany to the Phone model

1 like
cooperino's avatar

@Snapey sorry, I copied it wrong, I fixed the main post already (you can see my comment down with more data)

cooperino's avatar

@michaloravec @sergiu17 @silencebringer @snapey thank you.

I am sticking to conventions, however previous devs did not and I can not change it now (the plan is to change in the future). But I do write in singular. So now there are some mixed places:

The Users model has this:

    public function phone()
    {
        return $this->hasOne(Phone::class);
    }

The Phone model:

public function user()
    {
        return $this->belongsTo(Users::class, 'user_id');
    }

So even though not everything is according to convention, that's why there is the option to use foreign_key. But still it recognizes the plural term from some reason.

The queries that seem to fail are: (Inside the UsersObserver)

$users->phone->update(['user_id' => $users->id]);

and

$users->phone->id

But.. why? Why it says:

(PDOException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'phones.users_id'

Where does it see both as plural?

Snapey's avatar

this makes no sense

$users->phone->update(['user_id' => $users->id]);

find the phone associated with this user (by the foreign key) and set the foreign key to the same thing you found it by

so you could just delete this line?

1 like
cooperino's avatar

@Snapey In this part of the code the user has a phone_id but the Phone does not yet have the user_id saved, it's still NULL in this case. So it means if I get to that point something is wrong?

Because before I update the phone's user_id, I do some checks and only the I save the user_id for this phone

But now that I think of it, it's wrong since I can't use that relationship (yet) when the other one is still NULL, so I have to update it using query builder and not eloquent, right?

Snapey's avatar
Snapey
Best Answer
Level 122

@cooperino you should never have models with keys pointing to each other

1 like
cooperino's avatar

@Snapey makes sense, thank you. I changed it to

$phone = Phone::where('id', '=', $users->phone_id);
$phone->update(['user_id' => $users->id]);

Update: It's working

1 like
cooperino's avatar

@snapey I also changed my relationship to belongsTo on the Users instead of hasOne:

    public function phone()
    {
        return $this->belongsTo(Phone::class);
    }

and it now even works when I leave it as:

$users->phone->update(['user_id' => $users->id]);

In case of having both belongsTo, is it ok to leave the above? Or it's not a good practice and I should still not point like so and use

$phone = Phone::where('id', '=', $users->phone_id);
$phone->update(['user_id' => $users->id]);
Snapey's avatar

@cooperino Don't know why you even have a relationship to a single phone why not just put it in the users table?

Anyway, If the user has one phone number then I would have user_id on the phones table and nothing on the users table, with a relationship phone() on the User model and relationship user() on the Phone model

Please or to participate in this conversation.