skip to Main Content

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)}}" class="btn btn-primary">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)}}" 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. 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 AppHttpControllersadmin;


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

   
 
    
    

2

Answers


  1. 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

    Login or Signup to reply.
  2. //In folder model

    public function subfolder()
    {
        return $this->hasMany(Subfolder::class,'id','folder_id');
    }
    

    // In controller

    public function index(Request $request){
           $specificfolders = Folder::With('subfolder')->find($request->id);
    
    
           if(empty($specificfolders)){
                $subfolders = Subfolder::with('folder')->get();
                $folders = Folder::all();
          
                return view('admin.subfolder.index', compact('subfolders', 'folders'));
           }
           else {
                $subfolders = Subfolder::with('folder')->where('folder_id', $request->id)->get();
                $folders = Folder::all();
                return view('admin.subfolder.index', compact('subfolders', 'folders'));
           }
    
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search