Can't retrieve a one to many relationship in Laravel 4

帅比萌擦擦* 提交于 2019-12-12 02:28:09

问题


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.

My table names are users, profiles and profile_types.

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

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

class ProfileType extends Eloquent {

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

}

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

}

回答1:


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;


来源:https://stackoverflow.com/questions/18724773/cant-retrieve-a-one-to-many-relationship-in-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!