本文介绍了在Laravel 4中无法找到一对多的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过我的用户模型获取一个ProfileType,即$ user-> profile-> profiletype;但是,我无法检索对象。基本上,User hasOne Profile,Profile属于User和ProfileType。 ProfileType hasMany Profile。

I'm trying to get to a ProfileType through my User model, i.e. $user->profile->profiletype; I'm not able to retrieve an object, however. Basically, User hasOne Profile, and Profile belongsTo User and ProfileType. ProfileType hasMany Profile.

我的表名是用户,个人资料和profile_types。

My table names are users, profiles and profile_types.

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');
    }
}



models / Profile.php



models/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');
    }
}



models / ProfileType.php



models/ProfileType.php

class ProfileType extends Eloquent {

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

}



个人资料和ProfileType迁移



Profile and ProfileType migrations

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_types 的外键是 profile_type_id ,但实际上正在寻找 profiletype_id

I think you may have found a bug with the way Laravel handles foreign keys. It should know that the foreign key of profile_types is profile_type_id but it's actually looking for profiletype_id.

所以你可以更改表中的列,所以你不必担心每次需要另外的关系时发送额外的参数在该表格上,或在个人资料模型中,您可以使您的功能如此...

So you can either change that column in your table so you don't have to worry about sending extra parameters each time you need another relationship on that table, or in your Profile model, you can make your function like this...

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

然后,您应该可以找到用户的个人资料类型...

Then you should be able to find a user's profile type with...

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

这篇关于在Laravel 4中无法找到一对多的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 06:36