Я создаю страницу профиля пользователя и хочу получить данные из моей модели User и модели UserProfile. Но у меня проблема с получением результата. Вот что я сделал:
Модель пользователя
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'username',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/*
public function isAdmin() {
return $this->admin;
}
*/
public function profile() {
return $this->hasOne('App\UserProfile');
}
}
Модель UserProfile
class UserProfile extends Model
{
protected $table = 'user_profile';
protected $fillable = [
'phone',
'address'
];
public function user() {
return $this->belongsTo('App\User');
}
}
Затем я получаю доступ к отношению в моем ProfileController
public function getProfile($username) {
$user = User::with('user_profile')->where('username', $username)->get();
dd($user);
}
И я получил такую ошибку:
Call to undefined relationship [user_profile] on model [App\User].
user_profile - это имя моей таблицы
User
иUserProfile
мне также нужно иметь отношение в моемProfileController
? 01.02.2017with('')
имя таблицы, ха-ха. Спасибо за помощь 01.02.2017