簡體   English   中英

hasMany關系為null

[英]hasMany relationship is null

我是Laravel 5.0的新手。 我正在嘗試在兩個模型UserDevice之間建立倉促關系。 如果在修補App\\user::first()->device中我執行App\\user::first()->device則結果為null

我用一些虛擬數據填充了用戶和設備表,並使用了單個find()工作。 我在StackOverflow上進行了搜索,但找不到解決方案

這是我的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();
    });
}

我的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();
    });
}

和我在User Model功能

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

謝謝你們

代碼是正確的,我發現有時在代碼更改后需要重新啟動修補程序

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM