简体   繁体   English

Laravel 一页杂物 Select 全部来自 table2 其中 id = table1.id

[英]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.

相关问题 SELECT * FROM table1 INNER JOIN table2 ON table1.id = table2.id WHERE子句 - SELECT * FROM table1 INNER JOIN table2 ON table1.id=table2.id WHERE clause MySQL Select语句where table1.id!= table2.id - MySQL Select statement Where table1.id != table2.id 在 mysql 中选择 table1 和 table2 中的所有结果,其中两个表都有一个公共 ID - Select all results from table1 as well table2 in mysql where both table has one common id 连接两个表,其中table1.id等于table2.table1_id,但仅显示table1.id中在table2.table1_id中找不到的行 - Join two tables where table1.id equals table2.table1_id, but only display rows from table1.id that cannot be found in table2.table1_id 通过在Table1.id上应用groupBy,但在Table2上没有软删除的行,使用Laravel查询生成器来计算总和? - Calculate sum using Laravel Query Builder by applying groupBy on Table1.id but without soft deleted rows on Table2? PHP MYSQL - 突出显示数据库表行IF table1.id存在于table2中 - PHP MYSQL - Highlight a database table row IF table1.id exists in table2 Mysql - UPDATE表SET列= SELECT COUNT(*)FROM(SELECT * FROM table2 WHERE table2.id = table.id))不可能 - Mysql — UPDATE table SET column = SELECT COUNT(*) FROM ( SELECT * FROM table2 WHERE table2.id = table.id ) ) Impossible PHP MYSQL JOIN QUERY 2 数据库,其中 table1.id = table2.id,如何将 table2.content 显示为 table1.id - PHP MYSQL JOIN QUERY 2 Databases, Where table1.id = table2.id, How to display table2.content as table1.id PHP / MySQL-尝试选择两列,其中table1中的id等于table2中的id时出现问题 - PHP/MySQL - Trouble when trying to select two columns where id from table1 is equal to id from table2 当我使用此php时,结果显示所有表而不是table1.id = table2.id的特定条件 - When i use this php the results show all the table not the specific condition that table1.id = table2.id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM