我正在使用laravel 5.6 我有一个自引用表,称为“目录”
$table->increments('id');
$table->integer('parent_id')->unsigned()->default(null)->nullable();
$table->string('folder', 64);
$table->timestamps();
$table->unique(['parent_id', 'folder']);
对于根目录,“ parent_id”将为空
如何在web.php中创建资源路由?像这样:
Route::resource('directory.childdirectory','DirectoryController')->name('directory', 'dir_root_id', 'dir_child_id');
有没有针对自引用关系的示例CRUD?
如何在web.php中为自引用关系创建资源路由?
答案 0 :(得分:0)
简而言之,自引用表的问题是如何将“父”和“子” id传递给控制器。我通过在路由表(laravel 5.6的web.php)中定义了一个带有附加参数{id}
的资源控制器来做到这一点。Route::resource('{id}/directory', 'directoryController');
命令php artisan route:list看起来像下面的代码,其中{id}是父目录,{directory}是子目录
| POST | {id}/directory | directory.store
| GET|HEAD | {id}/directory | directory.index
| GET|HEAD | {id}/directory/create | directory.create
| GET|HEAD | {id}/directory/{directory} | directory.show
| PUT|PATCH | {id}/directory/{directory} | directory.update
| DELETE | {id}/directory/{directory} | directory.destroy
| GET|HEAD | {id}/directory/{directory}/edit | directory.edit