user9277271
user9277271

Reputation:

Laravel 5.8: SQLSTATE[42S22]: Column not found: 1054 Unknown column

I have a table called user_wallet like this:

enter image description here

Then at the Controller, I tried this:

try {
    if ($request->pay_wallet == '1') {
        $us_id = Auth()->user()->usr_id;
        $user_wallet = UserWallet::find('user_id', $us_id);
        dd($user_wallet);
    }
}catch (\Exception $e) {
    dd($e);
}

But I get this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '2' in 'field list' (SQL: select 2 from user_wallet where user_wallet.id = user_id limit 1)

However as you can see in the picture, there are two wallets with the user_id of 2.

So what's going wrong here? How can I solve this issue?

Upvotes: 0

Views: 1167

Answers (2)

Vahid Marali
Vahid Marali

Reputation: 136

the find method only works on id so it's better to use where clause

$user_wallet = UserWallet::where('user_id', $us_id);

Upvotes: 1

Aless55
Aless55

Reputation: 2709

I think you are using find() wrong, this method accepts only one paramater, it is the value of the primary key you want to find. Because you are in a pivot table and there is no id present, you will have to use firstWhere('user_id', $us_id) for the first occurrence or you will have to rewrite the find() method for the pivot table.

But please be aware that a user could have multiple wallets, so it might be better to ask them which wallet to use.

Upvotes: 0

Related Questions