Home > Blockchain >  Laravel one page crud Select all from table2 where id = table1.id
Laravel one page crud Select all from table2 where id = table1.id

Time:09-27

I am making a laravel application where i have 2 tables:

folder: id, name

subfolder: id, name, folder_id

'folder_id' is linked to the id of the folder table.

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.

folder.index:

 @foreach($folders as $folder)
        <tr>
            <td>{{$folder->id}}</td>
            <td>{{$folder->name}} </td>
            <td>
                <a href="{{ route('admin.subfolder.index',$folder->id)}}" >View {{$folder->name}}</a>
            </td>

subfolder.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)}}" >Edit</a>
            </td>
            <td>
                <form action="{{ route('admin.subfolder.destroy', $subfolder->id)}}" method="post">
                  @csrf
                  @method('DELETE')
                  <button  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. 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. i made an if statement because i have a one page crud page.

subfolder controller (removed unnecessary code) :

<?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'));
       }
    }

   
 
    
    

CodePudding user response:

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'));
       }


}

CodePudding user response:

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.

So you need to retrieve the id from the request, you can do something like this.

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.

You can find out more about retrieving inputs from request in the official Laravel documentation

  • Related