Laravel 5.8: 未找到列:1054 多对多关系中的未知列错误
Laravel 5.8: Column not found: 1054 Unknown column error in Many To Many relationship
我在用户模型和钱包模型之间存在多对多关系:
Wallet.php
:
public function users()
{
return $this->belongsToMany(User::class);
}
和User.php
:
public function wallets()
{
return $this->belongsToMany(Wallet::class);
}
我想像这样获取单个用户的钱包列表:
@forelse($user->wallets as $wallet)
<tr>
<td>{{ $wallet->id }}</td>
</tr>
@empty
<td colspan="5" class="text-center">No wallet exist</td>
@endforelse
但不知何故我得到了这个错误:
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)
但是这个用户 id 中的钱包已经存在于 user_wallet
table:
那么这里出了什么问题?我该如何解决这个问题?
非常感谢你们对此的任何想法或建议...
提前致谢。
尝试在关系
中提及枢轴table
public function users()
{
return $this->belongsToMany(User::class,'user_wallet','user_id','wallet_id');
}
和
public function wallets()
{
return $this->belongsToMany(Wallet::class,'user_wallet','wallet_id','user_id');
}
参考:https://laravel.com/docs/8.x/eloquent-relationships#many-to-many
我在用户模型和钱包模型之间存在多对多关系:
Wallet.php
:
public function users()
{
return $this->belongsToMany(User::class);
}
和User.php
:
public function wallets()
{
return $this->belongsToMany(Wallet::class);
}
我想像这样获取单个用户的钱包列表:
@forelse($user->wallets as $wallet)
<tr>
<td>{{ $wallet->id }}</td>
</tr>
@empty
<td colspan="5" class="text-center">No wallet exist</td>
@endforelse
但不知何故我得到了这个错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'user_wallet.user_usr_id' in 'field list' (SQL: select
wallets
.*,user_wallet
.user_usr_id
aspivot_user_usr_id
,user_wallet
.wallet_id
aspivot_wallet_id
fromwallets
inner joinuser_wallet
onwallets
.id
=user_wallet
.wallet_id
whereuser_wallet
.user_usr_id
= 373)
但是这个用户 id 中的钱包已经存在于 user_wallet
table:
那么这里出了什么问题?我该如何解决这个问题?
非常感谢你们对此的任何想法或建议...
提前致谢。
尝试在关系
中提及枢轴tablepublic function users()
{
return $this->belongsToMany(User::class,'user_wallet','user_id','wallet_id');
}
和
public function wallets()
{
return $this->belongsToMany(Wallet::class,'user_wallet','wallet_id','user_id');
}
参考:https://laravel.com/docs/8.x/eloquent-relationships#many-to-many