【问题标题】:Can't retrieve a one to many relationship in Laravel 4无法在 Laravel 4 中检索一对多关系
【发布时间】:2013-09-14 12:12:30
【问题描述】:

我正在尝试通过我的用户模型获取 ProfileType,即 $user->profile->profiletype;但是,我无法检索对象。基本上,User 有一个 Profile,而 Profile 属于 User 和 ProfileType。 ProfileType 有很多配置文件。

我的表名是 users、profiles 和 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 和 ProfileType 迁移

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

}

【问题讨论】:

  • 你也可以发布你的迁移吗?
  • 您是否尝试过使用类似 User::with('profile.profiletype')->get(); ?
  • @user1669496 我将迁移添加到问题中。

标签: php laravel laravel-4 relationship eloquent


【解决方案1】:

我想你可能已经发现了 Laravel 处理外键的方式的一个错误。它应该知道profile_types的外键是profile_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;

【讨论】:

  • 这确实是问题所在。将在 GitHub 上提出问题。
  • 也有类似结构的类似错误(问题和问题状态)。这个错误在 4.1.28 中仍然出现。
猜你喜欢
  • 1970-01-01
  • 2021-04-06
  • 2018-03-23
  • 2014-08-11
  • 1970-01-01
  • 2023-03-26
  • 2014-04-18
  • 2017-05-25
  • 2019-08-17
相关资源
最近更新 更多