Tenho muitos relacionamentos entre o Modelo do Usuário e o Modelo da Carteira:
Wallet.php :
public function users() {
return $this->belongsToMany(User::class,'user_wallet','user_id','wallet_id');
}
E User.php :
public function wallets() {
return $this->belongsToMany(Wallet::class,'user_wallet','user_id','wallet_id')->withPivot('balance');;
}
Então, no Blade, tentei isso:
@forelse($user->wallets as $wallet)
<tr>
<td>{{ $wallet->name }}</td>
<td>{{ $wallet->pivot->balance }}</td>
<td><a href="{{ route('user.WalletTransaction', ['walletId'=>$wallet->id,'userId'=>$wallet->users()->id]) }}" class="fa fa-exchange text-dark mt-1"></a></td>
</tr>
@empty
<td colspan="5" class="text-center">No wallet exist</td>
@endforelse
Como você pode ver, passei dois parâmetros como nome da rota, user.WalletTransaction
que é um link:
{{ route('user.WalletTransaction', ['walletId'=>$wallet->id,'userId'=>$wallet->users()->usr_id]) }}
E em web.php :
Route::get('wallet/transaction/{wallet}/{user}', 'Wallet\UserWalletController@WalletTransaction')->name('user.WalletTransaction');
Mas me mostra este erro:
Propriedade indefinida: Illuminate \ Database \ Eloquent \ Relations \ BelongsToMany :: $ usr_id
No entanto, usr_id
é o id dos usuários na users
mesa:
E a estrutura da tabela dinâmica user_wallet
é assim:
Então, como posso adicionar corretamente o ID do usuário a esse link?
Eu realmente apreciaria qualquer ideia ou sugestão de vocês sobre isso ...
Obrigado.
ATUALIZAÇÃO # 1 :
Resultado de <a href="{{ route('user.WalletTransaction', ['walletId'=>$wallet->id,'userId'=>$user->id]) }}" class="fa fa-exchange text-dark mt-1" />
mostra dois links!
$wallet->users()
retorna uma coleção, então você precisa iterar como se estivesse iterando wallets
@forelse($wallet->users() as $user)
<td>
<a href="{{ route('user.WalletTransaction', ['walletId'=>$wallet->id,'userId'=>$user->id]) }}" class="fa fa-exchange text-dark mt-1" />
</td>
[...]
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras