doukuangxun5382 2013-09-10 16:54
浏览 36
已采纳

无法在Laravel 4中检索一对多关系

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条回答 默认 最新

  • douqiang4501 2013-09-10 20:53
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?