为什么 BelongsTo() 在 Laravel 6 中不起作用?
Why BelongsTo() isn't working in Laravel 6?
我有两个模型 User
和 AccountType
class User extends Authenticatable
{
(...)
public function accountType()
{
return $this->belongsTo('App\AccountType', 'account_type_id', 'id');
}
}
class AccountType extends Model
{
protected $table = 'account_types';
// protected $primaryKey = 'id';
public function users()
{
return $this->hasMany('App\User');
}
}
我的数据库:
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();
});
Schema::create('account_types', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('account_type_name');
});
当我使用 hasMany 时 - 一切都很顺利,但是当我尝试使用 $user->accountType->account_type_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
和错误。当我注释掉 $user->accountType->account_type_name
时它工作正常。
ErrorException
Undefined offset: 1
详情在这里 - https://flareapp.io/share/jKPgOZPa
提前致谢!
您需要使用蛇形大小写而不是驼色大小写:
<td> {{ $user->account_type->account_type_name }} </td>
您似乎定义了错误的关系。您的用户模型应该是这样的:
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');
}
}
我已经用 <td> {{ App\User::find($user->id)->accountType->account_type_name }} </td>
替换了 <td> {{ $user->accountType->account_type_name }} </td>
,现在似乎一切正常。
你可以试试这个....我的工作完美..
$user->accountType->first() ->account_type_name
我有两个模型 User
和 AccountType
class User extends Authenticatable
{
(...)
public function accountType()
{
return $this->belongsTo('App\AccountType', 'account_type_id', 'id');
}
}
class AccountType extends Model
{
protected $table = 'account_types';
// protected $primaryKey = 'id';
public function users()
{
return $this->hasMany('App\User');
}
}
我的数据库:
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();
});
Schema::create('account_types', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('account_type_name');
});
当我使用 hasMany 时 - 一切都很顺利,但是当我尝试使用 $user->accountType->account_type_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
和错误。当我注释掉 $user->accountType->account_type_name
时它工作正常。
ErrorException
Undefined offset: 1
详情在这里 - https://flareapp.io/share/jKPgOZPa
提前致谢!
您需要使用蛇形大小写而不是驼色大小写:
<td> {{ $user->account_type->account_type_name }} </td>
您似乎定义了错误的关系。您的用户模型应该是这样的:
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');
}
}
我已经用 <td> {{ App\User::find($user->id)->accountType->account_type_name }} </td>
替换了 <td> {{ $user->accountType->account_type_name }} </td>
,现在似乎一切正常。
你可以试试这个....我的工作完美..
$user->accountType->first() ->account_type_name