Php 为什么BelongsTo()不是';你在拉威尔6号工作吗?
我有两种型号Php 为什么BelongsTo()不是';你在拉威尔6号工作吗?,php,laravel,laravel-6,Php,Laravel,Laravel 6,我有两种型号User和AccountType class User extends Authenticatable { (...) public function accountType() { return $this->belongsTo('App\AccountType', 'account_type_id', 'id'); } } 我的数据库: Schema::create('users', function (B
User
和AccountType
class User extends Authenticatable
{
(...)
public function accountType()
{
return $this->belongsTo('App\AccountType', 'account_type_id', 'id');
}
}
我的数据库:
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('account_type_id')->unsigned();
$table->foreign('account_type_id')->references('id')->on('account_types');
$table->string('full_name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
当我使用hasMany时,一切都像一个符咒,但当我尝试使用$user->accountType->account\u type\u name
时,它不起作用。用于打印用户的循环:
@foreach ($users as $user)
<tr>
<td>{{ $user->full_name }}</td>
<td>
<a href="#" class="badge badge-pill badge-warning">No team</a>
</td>
<td>
xyz
</td>
<td> {{ $user->accountType->account_type_name }} </td>
<td><a href="mailto:{{ $user->email }}">{{ $user->email }}</a></td>
<td>{{ $user->created_at }}</td>
</tr>
@endforeach
详情请参阅-
提前谢谢你 您需要使用蛇壳而不是骆驼壳:
<td> {{ $user->account_type->account_type_name }} </td>
{{$user->account\u type->account\u type\u name}
看起来您定义了错误的关系。您的用户模型应该如下所示:
class User extends Authenticatable
{
(...)
public function accountType()
{
return $this->hasOne('App\AccountType', 'account_type_id', 'id');
}
}
class AccountType extends Model
{
protected $table = 'account_types';
// protected $primaryKey = 'id';
public function user()
{
return $this->belongsTo('App\User');
}
}
和帐户类型模型应如下所示:
class User extends Authenticatable
{
(...)
public function accountType()
{
return $this->hasOne('App\AccountType', 'account_type_id', 'id');
}
}
class AccountType extends Model
{
protected $table = 'account_types';
// protected $primaryKey = 'id';
public function user()
{
return $this->belongsTo('App\User');
}
}
我已经用
{{App\user::find($user->id)->accountType->accountType\u name}}
替换了{{App\user::find($user->id)->accountType->account\u type\u name}}
,现在一切都正常了。你可以试试这个。。。。我的工作很好
$user->accountType->first() ->account_type_name
“你犯了什么错误?”DilipHirapara我把这些加在了我的问题上。抱歉:)所有用户在用户表中都有帐户类型id吗?是的,默认情况下他们都会得到。我不是说这是一个错误,但是如果在accountType模型中1)
accountType()
name应该是小写的“account\u type()”
2)返回$this->hasMany('App\User','account\u type\u id',id')代码>没有真正的帮助。尝试在blade.php中使用snake-case,在model和blade.phpIt中也一样。可能是某些用户没有有效的帐户类型。请尝试以下操作:{{$user->account\u-type->account\u-type\u-name???}
并查看是否有用户使用了空帐户类型这正是我现在拥有的帐户类型。问题是,我有两个用户,每个用户都有有效的user\u-type\u-id
什么user\u-type\u-id
是有效的,但是你在用户模型中建立了account\u-type\u-id
的关系;当然你是对的,我的意思是account\u type\u id
这样我们就有了一对一的关系,我希望实现一个account\u类型可以有多个用户,而一个用户只能有一个account\u类型。要让它工作$user->accountType->account\u type\u name
,你需要像我提到的那样定义一个关系;裁判:
$user->accountType->first() ->account_type_name