Laravel hasMany返回null

时间:2018-11-14 21:55:19

标签: php laravel

我的模特类别:

class Category extends Model
{
    protected $fillable = ['title', 'description', 'keywords', 'slug'];

    public function getRouteKeyName()
    {
        return 'slug';
    }

    public function products()
    {
        return $this->hasMany(Product::class);
    }
}

并有模型产品:

class Product extends Model
{

    protected $guarded = [];

    public function category()
    {
        return $this->belongsTo(Category::class);
    }
}

在餐桌产品中,我的产品带有列category_id。但是返回null。为什么?我无法使用$category->products获得当前类别的产品,因为它始终为空。我该如何解决?

在模型产品关系category中有效,但是在模型category中无效。.

在台式产品上,我具有级联:

$table->foreign('category_id')
    ->references('id')->on('categories')
    ->onDelete('cascade');

3 个答案:

答案 0 :(得分:1)

验证您的定义

return $this->hasMany('App\Product', 'category_id');

这里有错误,因为要附加在类别上,但是会出现在产品上

$table->foreign('category_id')
    ->references('id')->on('products')
    ->onDelete('cascade');

https://laravel.com/docs/5.6/eloquent-relationships#querying-relations

答案 1 :(得分:1)

一切都正确,只需在产品迁移中进行更改 更改此代码

<div class="masonry">
  <div class="item">
    <div class="item__content">
    </div>
  </div>
  <div class="item">
    <div class="item__content item__content--small">
    </div>
  </div>
  <div class="item">
    <div class="item__content item__content--medium">
    </div>
  </div>
  <div class="item">
    <div class="item__content item__content--small">
    </div>
  </div>
  <div class="item">
    <div class="item__content item__content--medium">
    </div>
  </div>
  <div class="item">
    <div class="item__content">
    </div>
  </div>
  <div class="item">
    <div class="item__content item__content--large">
    </div>
  </div>
  <div class="item">
    <div class="item__content item__content--medium">
    </div>
  </div>
  <div class="item">
    <div class="item__content item__content--small">
    </div>
  </div>
  <div class="item">
    <div class="item__content">
    </div>
  </div>
  <div class="item">
    <div class="item__content item__content--large">
    </div>
  </div>
  <div class="item">
    <div class="item__content">
    </div>
  </div>
  <div class="item">
    <div class="item__content item__content--small">
    </div>
  </div>
  <div class="item">
    <div class="item__content item__content--large">
    </div>
  </div>
  <div class="item">
    <div class="item__content item__content--medium">
    </div>
  </div>
  <div class="item">
    <div class="item__content item__content--small">
    </div>
  </div>
  <div class="item">
    <div class="item__content item__content--medium">
    </div>
  </div>
  <div class="item">
    <div class="item__content">
    </div>
  </div>
  <div class="item">
    <div class="item__content item__content--small">
    </div>
  </div>
</div>
@import url('https://fonts.googleapis.com/css?family=PT+Mono');

$bg: #4F000B;
$itemBg1: #720026;
$itemBg2: #CE4257;
$itemBg3: #FFC093;
$itemBg4: #FF7F51;
$counterBg: #222;

@mixin setColorAndHover($baseColor) {
  color: $baseColor;
  &:hover {
    background: lighten($baseColor, 8%);
  }
}

body,
html {
  position: relative;
  width: 100%;
  height: 100%;
  background: $bg;
  font-family: "PT Mono", monospace;
}

.masonry {
  column-count: 1;
  column-gap: 0;
  counter-reset: item-counter;

  @media screen and (min-width: 400px) {
    column-count: 2;
  }

  @media screen and (min-width: 600px) {
    column-count: 3;
  }

  @media screen and (min-width: 800px) {
    column-count: 4;
  }

  @media screen and (min-width: 1100px) {
    column-count: 5;
  }
}

.item {
  box-sizing: border-box;
  break-inside: avoid;
  padding: 10px;
  counter-increment: item-counter;

  &__content {
    position: relative;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 220px;
    font-size: 40px;
    color: darken($bg, 5%);
    background: currentColor;
    box-sizing: border-box;
    @include setColorAndHover($itemBg1);

    &:before {
      position: absolute;
      top: 0;
      left: 0;
      font-size: 13px;
      width: 2em;
      height: 2em;
      line-height: 2em;
      text-align: center;
      font-weight: bold;
      background-color: $counterBg;
      content: counter(item-counter);
    }

    &:after {
      color: darken($bg, 10%);
      content: 'ಠ‿ಠ';
    }

    &--small {
      @include setColorAndHover($itemBg2);
      height: 100px;

      &:after {
        content: '♥◡♥';
      }
    }

    &--medium {
      @include setColorAndHover($itemBg3);
      height: 175px;

      &:after {
        content: '◔ᴗ◔';
      }
    }

    &--large {
      @include setColorAndHover($itemBg4);
      height: 280px;

      &:after {
        content: 'ಠ_๏';
      }
    }
  }
}

$table->foreign('category_id')->references('id')
->on('categories')->onDelete('cascade');

答案 2 :(得分:1)

请进行以下更改

  1. 更改产品迁移文件(在“产品”表中)

     $table->integer('category_id')->unsigned();
     $table->foreign('category_id')->references('category_id')->on('categories');
    
  2. 然后更改类别模型

    public function products()
    {
        return $this->hasMany('App\Product','category_id');
    }
    

3。然后获得带有产品列表的所有类别

use App\Category   
             // include category model where you want to call this function
Category::with('products')->get();