Невозможно получить отношение от одного до многих в Laravel 4

Я пытаюсь добраться до ProfileType через мою модель User, то есть $ user-> profile-> profiletype; Однако я не могу получить объект. В основном пользователь имеет собственный профиль и профиль принадлежит пользователю и профилю. Профиль ProfileType hasMany.

Мои имена таблиц – это пользователи, профили и profile_types.

модели / User.php

use Cartalyst\Sentry\Users\Eloquent\User as SentryUserModel; class User extends SentryUserModel { /** * Indicates if the model should soft delete. * * @var bool */ protected $softDelete = true; public function profile() { return $this->hasOne('Profile'); } } 

модели / profile.php

 class Profile extends Eloquent { protected $fillable = array('username', 'slug', 'completed'); /** * @return */ public function user() { return $this->belongsTo('User'); } public function profiletype() { return $this->belongsTo('ProfileType'); } } 

модели / ProfileType.php

 class ProfileType extends Eloquent { /** * @return */ public function profiles() { return $this->hasMany('Profile'); } } 

Миграции профиля и профиля

 use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; // profile_types table class CreateProfileTypesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('profile_types', function(Blueprint $table) { $table->integer('id', true); $table->string('name'); $table->string('slug'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('profile_types'); } } // profiles table class CreateProfilesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('profiles', function(Blueprint $table) { $table->integer('id', true); $table->string('username'); $table->string('slug'); $table->boolean('completed'); $table->integer('user_id'); $table->integer('profile_type_id'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::drop('profiles'); } } 

Я думаю, вы, возможно, обнаружили ошибку с тем, как Laravel обрабатывает внешние ключи. Он должен знать, что внешний ключ profile_typesprofile_type_id но на самом деле он ищет profiletype_id .

Таким образом, вы можете либо изменить этот столбец в своей таблице, чтобы вам не приходилось беспокоиться о отправке дополнительных параметров каждый раз, когда вам нужны другие отношения в этой таблице, или в вашей модели Profile , вы можете сделать свою функцию следующим образом …

 function profiletype { return $this->belongsTo('ProfileType', 'profile_type_id'); } 

Затем вы сможете найти профиль профиля пользователя с помощью …

 $user = User::find(1); echo $user->profile->profiletype->name; echo $user->profile->profiletype->slug;