php - laravel 具有内部关系的多对多关系

标签 php mysql laravel relationship

我在获取有关项目的信息时遇到一些问题。我在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,但如果我想查看更多信息,它不起作用 $item->武器->名称

最佳答案

也许某些元素没有武器,请先检查武器是否存在:

@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

关于php - laravel 具有内部关系的多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51370156/

相关文章:

php - 在插入期间访问 mysql 插入的 ID

php - 创建数据库 DVWA 的问题

php - 1452 无法添加或更新子行 : a foreign key constraint fails Laravel 5. 4

laravel - API 服务器总是响应 "No ' Access-Control-Allow-Origin'"

php - 如何使用 css 将 .png 显示为 <h2> 和 <image> 的框架

php - Zend Framework 2 中与 DB 的动态连接

php - 构建跨两个表的 mysql 查询

php - PDO 错误是否出现在 Apache 的错误日志中?

php - 不知道如何将图像差异添加到 HTML 表的 php/sql 填充中

jquery - 如何在select 2 jquery中显示列表数据并查找数据