I want to get a value of balance column from a pivot table called user_wallet which goes like this:
So I wrote this at the Controller:
$bal = Wallet::with("users")->whereHas('users', function ($query) use ($wallet_id, $user_id) {
$query->where('wallet_id', $wallet_id);
$query->where('user_id', $user_id);
})->first();
But when I do dd($bal->balance), I get null!
So why is that? How can I get the balance value based on user_id and wallet_id properly?
Here is also the relationships between User model and Wallet model:
User.php
public function wallets()
{
return $this->belongsToMany(Wallet::class, 'user_wallet', 'user_id', 'wallet_id')->withPivot('balance');
}
Wallet.php
public function users()
{
return $this->belongsToMany(User::class, 'user_wallet', 'wallet_id', 'user_id');
}
I would really appreciate any idea or suggestion from you guys...
