Laravel 5.8:未定义 属性:Illuminate\Database\Eloquent\Relations\BelongsToMany::$usr_id
Laravel 5.8: Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$usr_id
我在用户模型和钱包模型之间有多对多关系:
Wallet.php:
public function users() {
return $this->belongsToMany(User::class,'user_wallet','user_id','wallet_id');
}
和User.php:
public function wallets() {
return $this->belongsToMany(Wallet::class,'user_wallet','user_id','wallet_id')->withPivot('balance');;
}
然后在 Blade,我尝试了这个:
@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
如您所见,我传递了两个参数作为路由名称 user.WalletTransaction
,即 link:
{{ route('user.WalletTransaction', ['walletId'=>$wallet->id,'userId'=>$wallet->users()->usr_id]) }}
然后 web.php:
Route::get('wallet/transaction/{wallet}/{user}', 'Wallet\UserWalletController@WalletTransaction')->name('user.WalletTransaction');
但它向我显示了这个错误:
未定义 属性: Illuminate\Database\Eloquent\Relations\BelongsToMany::$usr_id
然而 usr_id
是 users
table 的用户 ID:
主元 table 结构 user_wallet
是这样的:
那么我怎样才能正确地将用户 ID 添加到 link?
非常感谢你们对此的任何想法或建议...
谢谢。
UPDATE #1:
<a href="{{ route('user.WalletTransaction', ['walletId'=>$wallet->id,'userId'=>$user->id]) }}" class="fa fa-exchange text-dark mt-1" />
的结果显示两个 link!
$wallet->users()
returns 一个集合,所以你需要像迭代 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>
[...]
我在用户模型和钱包模型之间有多对多关系:
Wallet.php:
public function users() {
return $this->belongsToMany(User::class,'user_wallet','user_id','wallet_id');
}
和User.php:
public function wallets() {
return $this->belongsToMany(Wallet::class,'user_wallet','user_id','wallet_id')->withPivot('balance');;
}
然后在 Blade,我尝试了这个:
@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
如您所见,我传递了两个参数作为路由名称 user.WalletTransaction
,即 link:
{{ route('user.WalletTransaction', ['walletId'=>$wallet->id,'userId'=>$wallet->users()->usr_id]) }}
然后 web.php:
Route::get('wallet/transaction/{wallet}/{user}', 'Wallet\UserWalletController@WalletTransaction')->name('user.WalletTransaction');
但它向我显示了这个错误:
未定义 属性: Illuminate\Database\Eloquent\Relations\BelongsToMany::$usr_id
然而 usr_id
是 users
table 的用户 ID:
主元 table 结构 user_wallet
是这样的:
那么我怎样才能正确地将用户 ID 添加到 link?
非常感谢你们对此的任何想法或建议...
谢谢。
UPDATE #1:
<a href="{{ route('user.WalletTransaction', ['walletId'=>$wallet->id,'userId'=>$user->id]) }}" class="fa fa-exchange text-dark mt-1" />
的结果显示两个 link!
$wallet->users()
returns 一个集合,所以你需要像迭代 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>
[...]