[英]Laravel one page crud Select all from table2 where id = table1.id
I am making a laravel application where i have 2 tables:我正在制作一个 laravel 应用程序,其中有 2 个表:
folder: id, name文件夹:id,名称
subfolder: id, name, folder_id子文件夹:id、名称、folder_id
'folder_id' is linked to the id of the folder table. 'folder_id' 链接到文件夹表的 id。
Now i have a page with all the folder's.现在我有一个包含所有文件夹的页面。 and a click to see all the subfolders that have the same 'folder_id' as the 'id' of the folder column.
并单击以查看与文件夹列的“id”具有相同“folder_id”的所有子文件夹。
folder.index:文件夹索引:
@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>
subfolder.index:子文件夹.index:
@foreach($subfolders as $subfolder)
<tr>
<td>{{$subfolder->id}}</td>
<td>{{$subfolder->name}} </td>
<td>{{$subfolder->folder->name}} </td>
<td>
<a href="{{ route('admin.subfolder.edit',$subfolder->id)}}" class="btn btn-primary">Edit</a>
</td>
<td>
<form action="{{ route('admin.subfolder.destroy', $subfolder->id)}}" method="post">
@csrf
@method('DELETE')
<button class="btn btn-danger" type="submit">Delete</button>
</form>
</td>
</tr>
@endforeach
if i click on the folder.index button i go to the subfolder page with an number in the url.如果我单击 folder.index 按钮,我将 go 转到带有 url 中的数字的子文件夹页面。 the ID, but for some reason my $specificfolders query (SQL version: SELECT * FROM
subfolder
INNER JOIN folder on subfolder.folder_id = folder.id WHERE folder.id = (the id i give);) in the controller always return empty. ID,但由于某种原因,我的 $specificfolders 查询(SQL 版本:SELECT * FROM
subfolder
INNER JOIN folder on subfolder.folder_id = folder.id WHERE folder.id = (the id I give);)在 controller 中总是返回空。 i made an if statement because i have a one page crud page.我做了一个 if 语句,因为我有一个单页的 crud 页面。
subfolder controller (removed unnecessary code):子文件夹 controller(删除了不必要的代码):
<?php
namespace App\Http\Controllers\admin;
class SubfolderController extends Controller
{
public function index(Request $folder_id)
{ $specificfolders = DB::table('subfolder')->select('*')->join('folder', 'subfolder.folder_id', '=', 'folder.id')->where('folder.id', $folder_id)->get();
if($specificfolders->isEmpty())
{
$subfolders = Subfolder::with('folder')->get();
$folders = Folder::all();
return view('admin.subfolder.index', compact('subfolders', 'folders'));
}
else {
$subfolders = Subfolder::where('folder_id', $folder_id)->with('folder')->get();
$folders = Folder::all();
return view('admin.subfolder.index', compact('subfolders', 'folders'));
}
}
public function index(Request $folder_id)
$folder_id here is a Request and not a String containing the actual ID you are passing in the request.这里的 $folder_id 是一个请求,而不是包含您在请求中传递的实际 ID 的字符串。
So you need to retrieve the id from the request, you can do something like this.所以你需要从请求中检索 id,你可以这样做。
public function index(Request $request)
{ $specificfolders = DB::table('subfolder')->select('*')->join('folder', 'subfolder.folder_id', '=', 'folder.id')->where('folder.id', $request->get('id'))->get();
if($specificfolders->isEmpty())
{
$subfolders = Subfolder::with('folder')->get();
$folders = Folder::all();
return view('admin.subfolder.index', compact('subfolders', 'folders'));
}
else {
$subfolders = Subfolder::where('folder_id', $folder_id)->with('folder')->get();
$folders = Folder::all();
return view('admin.subfolder.index', compact('subfolders', 'folders'));
}
}
Assuming folder_id
is the name of the variable you are passing in you reqeust.假设
folder_id
是您在请求中传递的变量的名称。
You can find out more about retrieving inputs from request in the official Laravel documentation您可以在官方 Laravel 文档中找到有关从请求中检索输入的更多信息
public function index(Request $request){
$specificfolders = DB::table('subfolder')->select('*')->join('folder',
'subfolder.folder_id', '=', 'folder.id')->where('folder.id', $request->id)->get();
if($specificfolders->isEmpty())
{
$subfolders = Subfolder::with('folder')->get();
$folders = Folder::all();
return view('admin.subfolder.index', compact('subfolders', 'folders'));
}
else {
$subfolders = Subfolder::where('folder_id', $request->id)->with('folder')->get();
$folders = Folder::all();
return view('admin.subfolder.index', compact('subfolders', 'folders'));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.