【问题标题】:laravel Many To Many relationship with inner relationlaravel 多对多关系与内部关系
【发布时间】:2018-07-16 21:10:40
【问题描述】:

我在获取有关项目的信息时遇到了一些问题。我在论坛 stakoverflow 上阅读了很多信息。也许我找不到正确的信息。也许有人知道解决问题的方法 我有 4 张桌子

    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,但如果我想查看更多信息,它不起作用 $item->武器->名称

【问题讨论】:

  • 所以如果我理解正确,{{ $item-&gt;id }} 有效,但{{ $item-&gt;weapon-&gt;name }} 无效。当您尝试输出{{ $item-&gt;weapon-&gt;name }} 时会发生什么?你有错误吗?它只是空白吗?尝试输出{{ dd($item-&gt;weapon) }} 看看你得到了什么。
  • 请发布将 $item 变量发送到刀片视图的控制器方法。
  • @Tony 我添加了 dd(),我得到了所有信息,我拥有项目的所有属性,但我无法撤回它以查看 {{ $item-&gt;weapon-&gt;name }} 给出错误尝试获取属性名称'的非对象
  • @Polaris class DataBaseController extends Controller { public function index() { $categories = DbItemCategory::all(); return view('database.index')-&gt;withCategories($categories); } }
  • 在你的控制器中使用这个来获取类别; $categories = DbItemCategory::with('items.weapon')-&gt;all();

标签: php mysql laravel relationship


【解决方案1】:

可能有些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

【讨论】:

  • 是的,谢谢。我创建了 1 个没有武器的物品
  • @RolandVerner 绝对是那个项目的问题。
猜你喜欢
  • 2018-03-16
  • 2017-06-21
  • 2018-06-15
  • 2019-02-09
  • 1970-01-01
  • 2013-05-15
  • 2014-09-27
  • 1970-01-01
  • 2014-09-22
相关资源
最近更新 更多