duanfei9278 2017-01-20 11:14
浏览 41
已采纳

当尝试使用外键获取类型时,Laravel关系返回null

I am trying to fetch the type of user using the foreign key in the users table i.e. user_types_id but when I do it I just get null and cant get into the function userType in User Model. Any help would be much appreciated.

Thank you in advance. I have provided related models and tables

Controller

public function dashboard() {

    $userType = UserType::all();

    $id = Auth::user()->id;
    var_dump($id); // returns id

    $users = User::find($id);
    var_dump($user->user_types_id);  // returns user_type_id in users table

        if($users->userType){
            var_dump($users->userType->types); // error is in here. Not taking userType.
        } else {
            echo 'does not exit';
        }

        die();

    return view('dashboard', compact('users'));
}

User Model

    <?php
    namespace App;

    use Illuminate\Foundation\Auth\User as Authenticatable;
    use App\UserType;

    class User extends Authenticatable
    {
        /**
          * The attributes that are mass assignable.
          *
          * @var array
          */

        protected $fillable = [
            'username', 'password',
        ];

        /**
          * The attributes that should be hidden for arrays.
          *
          * @var array
          */

        protected $hidden = [
        'password', 'remember_token',
         ];

        public function userType() {
            return $this->belongsTo(UserType::class);
        }
    }

UserType Model

    <?php

    namespace App;
    use Illuminate\Database\Eloquent\Model;
    use App\User;

    class UserType extends Model
    {
        protected $fillable=['types'];

    public function users() {
        return $this->hasMany(User::class);
        }
    }

User Table

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_types_id')->unsigned()->index();
            $table->string('username')->unique();
            $table->string('password');
            $table->string('salt');
            $table->rememberToken();
            $table->timestamps();
        });
    }

UserType Table

    public function up()
    {
        Schema::create('user_types', function (Blueprint $table) {
            $table->increments('id');
            $table->string('types');
            $table->timestamps();
        });
    }
  • 写回答

1条回答 默认 最新

  • dongpu42006096 2017-01-20 11:40
    关注

    You've named your relation userType, therefore Eloquent assumes that the foreign key is called user_type_id, not user_types_id.

    Replace

    return $this->belongsTo(UserType::class);
    

    with

    return $this->belongsTo(UserType::class, 'user_types_id');
    

    to tell Eloquent the name of foreign key column and it should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到