我使用默認用戶模型,我創建了一個UserStatus型號:默認用戶模型和Relatinship - Laravel 5.4
用戶
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function status()
{
return $this->belongsTo('App\UserStatus');
}
}
UserStatus
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserStatus extends Model
{
public $timestamps = false;
public function users()
{
return $this->hasMany('App\User');
}
}
我不能獲得相關的模型屬性,因爲$用戶>狀態是NULL:
$users = User::all();
foreach ($users as $user) {
var_dump($user->status->name);
}
什麼是最好的做法/解決方案?
謝謝!
**調用未定義的關係[狀態]型號[ Illuminate \ Foundation \ Auth \ User]。** – Peter
則意味着您的用戶模型有問題。使用** Illuminate \ Database \ Eloquent \ Model **擴展用戶類以查看它是否可行。 – oseintow