前提・実現したいこと
【前提】
categoriesテーブル
id | name |
---|
listsテーブル
id | name | ... |
---|
categories_listsテーブル(中間テーブル)
id | category_id | list_id |
---|
【実現したいこと】
・folderの中に該当するcategoryを。categoryの中に該当するlistを表示させ表示させたい。(今回、categoryの中に該当するlistを表示させる段階でつまずいております。)
該当のソースコード
board.blade.php
php
1@foreach($folders as $folder) 2<li class="mb-4"> 3 <div class="parent folder-name" data-toggle="collapse" data-target="#target{{ $folder->id }}" aria-expand="false" aria-controls="#target{{ $folder->id }}"><i class="fas fa-caret-right mr-3 triangle"></i>{{ $folder->name }}</div> 4 <ul class="create-category-items p-0"> 5 @foreach($categories as $category) 6 @if($folder->id == $category->folder_id) 7 <li class="mt-3"> 8 <div class="category-left d-flex align-items-center" data-toggle="collapse" data-target="#target{{ $folder->id }}-{{ $category->id }}" aria-expand="false" aria-controls="#target{{ $folder->id }}-{{ $category->id }}"> 9 <i class="fas fa-caret-right mr-3 triangle"></i> 10 <p class="m-0">{{ $category->name }}</p> 11 </div> 12 <div class="child collapse" id="target{{ $folder->id }}-{{ $category->id }}"> 13 <ul class="p-0"> 14 15 <!-- 該当の箇所 --> 16 @foreach($category->lists as $list) 17 <li class="list-name mt-2"><a href="#">{{ $list->pivot->name }}</a></li> 18 @endforeach 19 20 </ul> 21 </div> 22 </li> 23 @endif 24 @endforeach 25 </ul> 26 </div> 27</li> 28@endforeach 29
Controller.php
php
1public function board(Request $request) 2{ 3 $folders = Folder::all(); 4 $categories = Category::all(); 5 $lists = ListItem::all(); 6 return view("/admin/board", [ 7 'folders' => $folders, 8 'categories' => $categories, 9 'lists' => $lists, 10 ]); 11} 12
Category.php
php
1class Category extends Model 2{ 3 public function folder() 4 { 5 return $this->belongsTo('App\Models\Folder'); 6 } 7 8 public function lists() 9 { 10 return $this->belongsToMany('App\Models\listItem', 'categories_lists', 'list_id', 'category_id')->withTimestamps()->withPivot('category_id', 'list_id'); 11 } 12
ListItem.php(Listテーブルのモデルです。)
php
1class ListItem extends Model 2{ 3 // Listというモデル名は使用不可のためListItemというモデル名に 4 protected $table = 'lists'; 5 6 public function categories() 7 { 8 return $this->belongsToMany('App\Models\Category', 'categories_lists', 'list_id', 'category_id')->withTimestamps()->withPivot('category_id', 'list_id'); 9 } 10
試したこと
多対多のリレーションについていろいろ調べましたが、解決に至らなかったため質問させていただきました。
具体的な解決法だけではなく、参考になりそうな記事でもよいので解決に導いていただければ幸いです。よろしくお願いします。
追記
migrationです。
categoriesテーブル
php
1class CreateCategoriesTable extends Migration 2{ 3 public function up() 4 { 5 Schema::create('categories', function (Blueprint $table) { 6 $table->increments('id'); 7 $table->integer('folder_id'); 8 $table->string('name'); 9 $table->timestamps(); 10 }); 11 } 12
listsテーブル
php
1class CreateListsTable extends Migration 2{ 3 public function up() 4 { 5 Schema::create('lists', function (Blueprint $table) { 6 $table->increments('id'); 7 $table->string('name'); 8 $table->integer('point'); 9 $table->text('description'); 10 $table->text('relation'); 11 $table->text('checklist'); 12 $table->integer('status'); 13 $table->timestamps(); 14 }); 15 } 16
categories_listsテーブル
php
1class CreateCategoriesListsTable extends Migration 2{ 3 public function up() 4 { 5 Schema::create('categories_lists', function (Blueprint $table) { 6 $table->increments('id'); 7 $table->integer('category_id'); 8 $table->integer('list_id'); 9 $table->timestamps(); 10 }); 11 } 12