Php Can';在Laravel 4中检索不到一对多关系

Php Can';在Laravel 4中检索不到一对多关系,php,laravel,laravel-4,relationship,eloquent,Php,Laravel,Laravel 4,Relationship,Eloquent,我试图通过我的用户模型获得一个ProfileType,即$User->profile->ProfileType;但是,我无法检索对象。基本上,用户只有一个概要文件,概要文件属于用户和概要文件类型。ProfileType有许多配置文件 我的表名是用户、配置文件和配置文件类型 models/User.php models/Profile.php models/ProfileType.php 配置文件和配置文件类型迁移 我想你可能已经发现了拉威尔处理外键的方式有问题。它应该知道,profile\u-t

我试图通过我的用户模型获得一个ProfileType,即$User->profile->ProfileType;但是,我无法检索对象。基本上,用户只有一个概要文件,概要文件属于用户和概要文件类型。ProfileType有许多配置文件

我的表名是用户、配置文件和配置文件类型

models/User.php models/Profile.php models/ProfileType.php 配置文件和配置文件类型迁移
我想你可能已经发现了拉威尔处理外键的方式有问题。它应该知道,
profile\u-types
的外键是
profile\u-type\u-id
,但实际上它在查找
profiletype\u-id

因此,您可以更改表中的该列,这样您就不必担心每次需要该表上的另一个关系时都会发送额外的参数,或者在
配置文件
模型中,您可以使函数如下所示

function profiletype
{
    return $this->belongsTo('ProfileType', 'profile_type_id');
}
然后你应该能够找到一个用户的配置文件类型与

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

您也可以发布迁移吗?您是否尝试过使用类似User::with('profile.profiletype')->get()的方法加载迁移@user1669496我将我的迁移添加到问题中。这确实是问题所在。将在GitHub上产生问题。还有一个结构类似的类似bug(issue&IssueStatus)。此错误仍然出现在4.1.28中。
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');
    }

}
function profiletype
{
    return $this->belongsTo('ProfileType', 'profile_type_id');
}
$user = User::find(1);
echo $user->profile->profiletype->name;
echo $user->profile->profiletype->slug;