0

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?

2 Answers 2

1

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

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

2 Comments

Call to undefined method App\UserWallet::firstWhere()
@tejoslaeslio: I guess you use laravel version below 6.*, then use UserWallet::where('user_id', $us_id)->first();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.