【发布时间】:2022-09-27 16:46:24
【问题描述】:
所以我正在做一个 laravel 项目,但我被困在这一部分:
我有一个包含 2 个表的数据库:\'folder\' 和 \'subfolder\'
我对他们俩都有一个有效的工作,我还包括了 hasMany 和 belongsTo。这样我就可以在子文件夹视图中看到文件夹名称。
现在我想在文件夹视图中创建一个按钮,以便我可以查看与文件夹上的 \'id\' 具有相同 \'folder_id\' 的所有子文件夹。我已经有一个使用 \'id\' 值重定向的按钮,但我仍然看到所有子文件夹,而不是 folder_id = id 的子文件夹。
折叠桌:
Schema::create(\'folder\', function (Blueprint $table) {
$table->engine = \'InnoDB\';
$table->increments(\"id\")->unsigned(false);
$table->string(\'name\');
$table->timestamps();
});
子文件夹表:
Schema::create(\'subfolder\', function (Blueprint $table) {
$table->engine = \'InnoDB\';
$table->increments(\"id\")->unsigned(false);
$table->string(\'name\');
$table->unsignedInteger(\'folder_id\')->value(11)->unsigned(false)->nullable();
$table->foreign(\'folder_id\')->references(\'id\')->on(\'folder\');
$table->timestamps();
});
文件夹索引:
@foreach($folders as $folder)
<tr>
<td>{{$folder->id}}</td>
<td>{{$folder->name}} </td>
<td>
<a href=\"{{ route(\'admin.subfolder.index\',$folder->id)}}\" class=\"btn btn-primary\">View {{$folder->name}}</a>
</td>
<td>
<a href=\"{{ route(\'admin.folder.edit\',$folder->id)}}\" class=\"btn btn-primary\">Edit</a>
</td>
<td>
<form action=\"{{ route(\'admin.folder.destroy\', $folder->id)}}\" method=\"post\">
@csrf
@method(\'DELETE\')
<button class=\"btn btn-danger\" type=\"submit\">Delete</button>
</form>
</td>
</tr>
@endforeach
子文件夹控制器索引:
$subfolders = Subfolder::with(\'folder\')->get();
$folders = Folder::all();
return view(\'admin.subfolder.index\', compact(\'subfolders\', \'folders\'));
如果我需要添加任何信息,我很乐意这样做!