Laravel 5 Laravel 5.4雄辩的一对多关系

Laravel 5 Laravel 5.4雄辩的一对多关系,laravel-5,eloquent,Laravel 5,Eloquent,我正在刷新我对拉威尔雄辩的人际关系的认识。不幸的是,我无法解决/发现一对多关系中的问题。有人对我的代码有什么想法/建议吗 发生了什么事 Eloquent没有从mobiles表中找到任何相关数据 应该发生什么 从移动表获取所有相关数据 涉及代码 我正在进行一个干净的Laravel 5.4安装。这是我创建/添加的唯一代码 用户模型 public function phone(){ return $this->hasMany('App\Mobile', 'user_id', 'id')

我正在刷新我对拉威尔雄辩的人际关系的认识。不幸的是,我无法解决/发现一对多关系中的问题。有人对我的代码有什么想法/建议吗

发生了什么事

Eloquent没有从mobiles表中找到任何相关数据

应该发生什么

从移动表获取所有相关数据

涉及代码 我正在进行一个干净的Laravel 5.4安装。这是我创建/添加的唯一代码

用户模型

public function phone(){

    return $this->hasMany('App\Mobile', 'user_id', 'id');

}
使用此函数,eloquent将从移动表中获取所有记录,其中列user_id==id对吗

用户表

Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
 Schema::create('mobiles', function (Blueprint $table) {
        $table->increments('id');

        $table->integer('user_id');

        $table->string('phoneNumber');

        $table->timestamps();
    });
protected $fillable=['name', 'email' , 'password'];

public function Mobile(){

    return $this->hasMany(Mobile::class);

}
protected $fillable=['user_id', 'phonenumber'];

public function User(){

    return $this->hasMany(User::class);

}
移动台

Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
 Schema::create('mobiles', function (Blueprint $table) {
        $table->increments('id');

        $table->integer('user_id');

        $table->string('phoneNumber');

        $table->timestamps();
    });
protected $fillable=['name', 'email' , 'password'];

public function Mobile(){

    return $this->hasMany(Mobile::class);

}
protected $fillable=['user_id', 'phonenumber'];

public function User(){

    return $this->hasMany(User::class);

}

根据您的代码,我认为问题在于您的模型。应该是这样的:

用户表

Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
 Schema::create('mobiles', function (Blueprint $table) {
        $table->increments('id');

        $table->integer('user_id');

        $table->string('phoneNumber');

        $table->timestamps();
    });
protected $fillable=['name', 'email' , 'password'];

public function Mobile(){

    return $this->hasMany(Mobile::class);

}
protected $fillable=['user_id', 'phonenumber'];

public function User(){

    return $this->hasMany(User::class);

}
移动台

Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
 Schema::create('mobiles', function (Blueprint $table) {
        $table->increments('id');

        $table->integer('user_id');

        $table->string('phoneNumber');

        $table->timestamps();
    });
protected $fillable=['name', 'email' , 'password'];

public function Mobile(){

    return $this->hasMany(Mobile::class);

}
protected $fillable=['user_id', 'phonenumber'];

public function User(){

    return $this->hasMany(User::class);

}
您需要在模型上指定可填充或保护属性,因为默认情况下,所有有说服力的模型都可以防止质量分配


假设您是从数据库正确调用的,这应该会起作用。

正如@Maraboc在评论部分所建议的,使用内容创建一个
user
函数

public function user(){
    $this->belongsTo(User::class);
}

这解决了我的问题

并且在您的
Mobil
模型中使用
public function user(){return$This->belongsTo('App\user');}
并在('users')上添加
$table->foreign('user_id')->引用('id')->mobiles
migration中的code>谢谢,不幸的是它不起作用。我将再次开始学习雄辩的人际关系。你是如何获得结果的?查询是什么?我使用user::find()选择一个用户;我叫->电话();关于查找的结果。