laravel多对多关系与内部关系

woobm2wo  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(385)

我在获取商品信息时遇到了一些问题。我在stakoverflow论坛上读到了很多信息。也许我找不到正确的信息。也许有人知道要解决这个问题我有4张table

public function up()
{
    Schema::create('db_items', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
    });
}
public function up()
    {
        Schema::create('db_item_weapons', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('item_id')->unsigned();
            $table->foreign('item_id')
                ->references('id')->on('db_items')
                ->onDelete('cascade');
            $table->integer('grade_id')->unsigned();
            $table->foreign('grade_id')
                ->references('id')->on('db_item_grades')
                ->onDelete('cascade');
            $table->string('hand')->nullable();
            $table->string('name')->nullable();
            $table->text('description')->nullable();
            $table->string('icon')->nullable();
            $table->string('p_atak')->nullable();
            $table->timestamps();
        });
    }
public function up()
{
    Schema::create('db_item_categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name')->nullable();
        $table->timestamps();
    });
}
public function up()
{
    Schema::create('db_item_db_item_category', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('item_id')->unsigned()->index();
        $table->foreign('item_id')
            ->references('id')->on('db_items')
            ->onDelete('cascade');

$table->integer('category_id')->unsigned()->index();
            $table->foreign('category_id')
                ->references('id')->on('db_item_categories')
                ->onDelete('cascade');
            $table->timestamps();
        });
    }

和3个模型

class DbItem extends Model
{
    function weapon()
    {
        return $this->hasOne(DbItemWeapon::class, 'item_id');
    }

    function categories()
    {
        return $this->belongsToMany(DbItemCategory::class,'db_item_db_item_category','item_id','category_id');
    }
}
class DbItemWeapon extends Model
{
    function DbItem()
    {
        return $this->belongsTo(DbItem::class);
    }

}
class DbItemCategory extends Model
{
    function items()
    {
        return $this->belongsToMany(DbItem::class,'db_item_db_item_category','category_id','item_id')->with('weapon');
    }
}

例如,当我试图得到一些信息时,它就起作用了

@foreach($categories as $category)
 <li>
   <a class="uk-accordion-title" href="#">{{ $category->name }}</a>
      <div class="uk-accordion-content">
         @foreach( $category->items as $item)
            <p>{{ $item->id }}</p>
         @endforeach
      </div>
 </li>
@endforeach

我得到了与他们的项目类别,我可以查看项目id,其中包含在categoryes,但如果我想看到更多的信息,它不工作$item->武器->名称

xwmevbvl

xwmevbvl1#

也许是一些 items 没有一个 weapon ,检查 weapon's 存在优先:

@foreach($categories as $category)
 <li>
   <a class="uk-accordion-title" href="#">{{ $category->name }}</a>
      <div class="uk-accordion-content">
         @foreach( $category->items as $item)
            @php
                $weapon = $item->weapon;
            @endphp
            <p>{{ $item->id }}</p>
            <p>{{ $weapon ? $weapon->name : null }}</p>
         @endforeach
      </div>
 </li>
@endforeach

相关问题