drzbc6003 2018-07-16 21:10
浏览 53
已采纳

laravel与内在关系的多对多关系

I have some problem to get information about item. I read a lot of information on forums stakoverflow. Maybe i can not find the correct information. Maybe someone knows about that hove to solve the problem I have 4 tables

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

And 3 models

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');
    }
}

When i try to get some inforamtion it wotrks for example

@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

i got the categories wtith their items and i can view the items id which contains in categoryes but if i want to see more information, its not work $item->weapon->name

  • 写回答

1条回答 默认 最新

  • duanlu1950 2018-07-17 07:27
    关注

    Maybe some of the items does not have a weapon, check for the weapon's existence first:

    @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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法