繁体   English   中英

Laravel属于和有很多不返回数据

[英]Laravel belongsTo and hasMany not returning data

我是laravel / eloquent的新手,到目前为止非常喜欢它们,但是我在获取belongsTo和让很多人工作方面遇到困难。 我已经读完了其他人遇到的许多问题,但是还没有找到可以为我解决问题的解决方案。

我有一个“页面”模型和一个“类别”模型。 这是表的定义:

Schema::create('pages', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

Schema::create('categories', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('page_id');
    $table->foreign('page_id')->references('id')->on('pages');
    $table->string('name');
    $table->timestamps();
});

这是我的Page模型:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Page extends Model
{
    public function categories()
    {
        return $this->hasMany('App\Category');
    }
}

这是我的类别模型:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    public function page() 
    {
        return $this->belongsTo('App\Page');
    }
}

这是一些修补的结果:

> php artisan cache:clear
Application cache cleared!
> php artisan tinker
>>> \App\Category::find(1)
=> App\Category {#2916
     id: "1",
     page_id: "1",
     name: "First Category",
     created_at: "2018-12-30 03:18:07.347",
     updated_at: "2018-12-30 03:18:07.347",
   }
>>> \App\Category::find(1)->page()
=> Illuminate\Database\Eloquent\Relations\BelongsTo {#2918}
>>> \App\Page::find(1)
=> App\Page {#2902
     id: "1",
     name: "First Page",
     created_at: "2018-12-30 03:18:07.220",
     updated_at: "2018-12-30 03:18:07.220",
   }
>>> \App\Page::find(1)->categories()
=> Illuminate\Database\Eloquent\Relations\HasMany {#2922}
>>>

我的对象很好,但是当我尝试建立关系时,我得到的似乎是空对象:

=> Illuminate\\Database\\Eloquent\\Relations\\BelongsTo {#2918}

=> Illuminate\\Database\\Eloquent\\Relations\\HasMany {#2922}

如果我将page()和category()函数换成类似以下的函数,则一切正常:

public function page() 
{
    return \App\Page::find($this->page_id);
}

public function categories()
{
    return \App\Category::where('page_id', $this->id)->get();
}

结果如下:

> php artisan cache:clear
Application cache cleared!
> php artisan tinker
Psy Shell v0.9.9 (PHP 7.3.0 — cli) by Justin Hileman
>>> \App\Page::find(1)
=> App\Page {#2916
     id: "1",
     name: "First Page",
     created_at: "2018-12-30 03:18:07.220",
     updated_at: "2018-12-30 03:18:07.220",
   }
>>> \App\Page::find(1)->categories()
=> Illuminate\Database\Eloquent\Collection {#2918
     all: [
       App\Category {#2921
         id: "1",
         page_id: "1",
         name: "First Category",
         created_at: "2018-12-30 03:18:07.347",
         updated_at: "2018-12-30 03:18:07.347",
       },
       App\Category {#2922
         id: "2",
         page_id: "1",
         name: "Second Category",
         created_at: "2018-12-30 03:18:07.480",
         updated_at: "2018-12-30 03:18:07.480",
       },
       App\Category {#2923
         id: "3",
         page_id: "1",
         name: "Third Category",
         created_at: "2018-12-30 03:18:07.623",
         updated_at: "2018-12-30 03:18:07.623",
       },
     ],
   }
>>> \App\Category::find(1)
=> App\Category {#2904
     id: "1",
     page_id: "1",
     name: "First Category",
     created_at: "2018-12-30 03:18:07.347",
     updated_at: "2018-12-30 03:18:07.347",
   }
>>> \App\Category::find(1)->page()
=> App\Page {#2921
     id: "1",
     name: "First Page",
     created_at: "2018-12-30 03:18:07.220",
     updated_at: "2018-12-30 03:18:07.220",
   }
>>>

编辑:

我发现如果我添加->get()我会按期望的方式看到数据:

public function page() 
{
    return $this->belongsTo('App\Page')->get();
}

但是,我所看到的所有示例都没有显示任何这样做的例子。 我只是误解了它应该如何工作? 我希望在修补$ page-> categories()的同时进行; 会给我类别,而无需执行$ page-> categories()-> get();

尝试这个。

    Schema::create('categories', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('page_id')->unsigned();
    $table->foreign('page_id')->references('id')->on('pages');
    $table->string('name');
    $table->timestamps();
});

嗯,像往常一样,我发现这很简单。

调用$page->categories作为属性会给我数据

调用$page->categories()作为函数将返回一个对象,我可以继续将其他命令链接到该对象。

万一它对任何人都有帮助,我可以通过回顾非常有用的laracasts视频找到解决方案。 特别是关于雄辩性关系的一集,其中我刚刚发布的内容得到了明确解释: https//laracasts.com/series/laravel-from-scratch-2018/episodes/16

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM