Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

RolandV's avatar

laravel Many To Many relationship with inner relation

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->integer('сrystals')->nullable();
        $table->integer('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);
    }

    function grade()
    {
        return $this->hasOne(DbItemGrade::class, 'id','grade_id');
    }

}
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

Help please

0 likes
1 reply

Please or to participate in this conversation.