Search code examples
phplaravel-5has-many

hasMany relationship is null


I'm new to Laravel 5.0. I'm trying to make a hasmany relationship between my two models, User and Device. If in tinker I do App\user::first()->device the result is null

I populated the users and devices tables with some dummy data and the single find() work. I searched on StackOverflow but I could't find out a solution

Here's my users table

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

my devices table

public function up()
{
    Schema::create('devices', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->double('lat', 15, 8);
        $table->double('lon', 15, 8);
        $table->timestamps();
    });
}

and my function in the User Model

public function device()
{
return  $this->hasMany('App\Device');
}

Thank you all


Solution

  • The code was right, I found out sometimes tinker needs to be rebooted after the changes in the code