I'm refreshing my knowledge about Laravel eloquent relationships. Unfortunately, i'm unable to solve/find a problem with a one to many relationship. Someone any idea/suggestion what is wrong with my code?
What is happening
Eloquent does not find any related data from the mobiles table. Current result (pastebin)
What should happen
Getting all related data from the mobile table.
Involved code
I'm working on a clean Laravel 5.4 installation. This is the only code i created/added.
User model
public function phone(){
return $this->hasMany('App\Mobile', 'user_id', 'id');
}
With this function eloquent will get all records from the mobile table where column user_id == id right?
User table
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Mobile table
Schema::create('mobiles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('phoneNumber');
$table->timestamps();
});
Mobil
model usepublic function user() { return $this->belongsTo('App\User'); }
and add$table->foreign('user_id')->references('id')->on('users');
in themobiles
migration – Maraboc