1

I have a Many To Many relationship between User Model & Wallet Model:

Wallet.php:

public function users()
    {
        return $this->belongsToMany(User::class);
    }

And User.php:

public function wallets()
    {
        return $this->belongsToMany(Wallet::class);
    }

And I want to get wallet list of a single user like this:

@forelse($user->wallets as $wallet)
<tr>
   <td>{{ $wallet->id }}</td>
</tr>
@empty
<td colspan="5" class="text-center">No wallet exist</td>
@endforelse

But I get this error somehow:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_wallet.user_usr_id' in 'field list' (SQL: select wallets.*, user_wallet.user_usr_id as pivot_user_usr_id, user_wallet.wallet_id as pivot_wallet_id from wallets inner join user_wallet on wallets.id = user_wallet.wallet_id where user_wallet.user_usr_id = 373)

However the wallet within this user id exists already at user_wallet table:

enter image description here

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

I would really appreciate any idea or suggestion from you guys about this...

Thanks in advance.

John Lobo
  • 14,355
  • 2
  • 10
  • 20

1 Answers1

0

Try Mentioning pivot table in relationship

public function users()
{
        return $this->belongsToMany(User::class,'user_wallet','user_id','wallet_id');
}

and

public function wallets()
{
        return $this->belongsToMany(Wallet::class,'user_wallet','wallet_id','user_id');
}

Ref:https://laravel.com/docs/8.x/eloquent-relationships#many-to-many

John Lobo
  • 14,355
  • 2
  • 10
  • 20
  • Still `SQLSTATE[42S22]: Column not found: 1054 Unknown column` –  Jul 14 '21 at 05:28
  • That was great, thank u, but how to get the `wallet` name based on the `wallet_id` –  Jul 14 '21 at 05:34
  • Wallet name is stored at `wallets` table. I had put the information of Migrations in this question: https://stackoverflow.com/questions/68372299/how-to-find-data-based-on-many-to-many-relationship-in-laravel-5-8 –  Jul 14 '21 at 05:34
  • @loctoj since its seperate issue .so i have updated in that question – John Lobo Jul 14 '21 at 05:40
  • Hey dude, I wonder if both belongsToMany relationships defined with with the same keys in the same order ie 'user_wallet','user_id','wallet_id' are correct or not? –  Jul 14 '21 at 08:11
  • @loctoj just reverse it based on current model if its error.i usaully not using since i follow laravel naming conventio.let me know if that issue so i can update post – John Lobo Jul 14 '21 at 08:13
  • if that causing then public function wallets() { return $this->belongsToMany(Wallet::class,'user_wallet','wallet_id','user_id'); } – John Lobo Jul 14 '21 at 08:14
  • Yeah I think it should be updated for other people not to get confused. –  Jul 14 '21 at 08:21
  • But wait! I tested it and wallet details of a user who has a record on `user_wallet` table does not appear when changing keys of `wallets()` function in User model ! Seems like the first answer was correct. It looks strange somehow. –  Jul 14 '21 at 08:33
  • @loctoj if you want only those record which has walet or visa versa then you have to use has. i have tested it .so no more issues regarding relationship – John Lobo Jul 14 '21 at 08:46
  • @loctoj to retrieve only those record who has wallet then your query should add has('relationname') – John Lobo Jul 14 '21 at 08:56
  • Plz checkout this: https://stackoverflow.com/questions/68375097/laravel-many-to-many-relationship-does-not-show-data –  Jul 14 '21 at 09:03
  • @loctoj check my updated answer and delete that new post – John Lobo Jul 14 '21 at 09:06
  • Thank u brother. God saves you <3 –  Jul 14 '21 at 09:11