Danny971's avatar

why am I getting Undefined variable $files

hello I am trying to pull data from my database and display it on a table on my view

here is my code

The Controller


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\upload;

class FileController extends Controller
{

    public function uploadIndex() {
        $files = upload::where('family_id', $id)->get();
        dd($files);
        return view('profile')->with('files', $files);
    }

    public function uploadFile(Request $request)
    {

       $request -> validate ([
        'file' => 'required|mimes:png,jpeg,pdf|max:2048',
       
       ]);

       $fileModel = new upload;
    
       if($request -> file()) {

        $fileName = time(). '_'.$request -> file ->getClientOriginalName();
        $filePath = $request -> file ('file') -> storeAs('uploads', $fileName, 'public');
        $fileModel -> filename = time(). '_'. $request -> file -> getClientOriginalName();
        $fileModel -> document = '/storage/' . $filePath;
        $fileModel -> family_id = $request -> input('family_id');
        $fileModel ->save();

      
        return redirect()->route('profile', ['id' => $request->input('family_id')])
        ->with('success', 'File has been uploaded')
        ->with('file', $fileName);
}
}
}

the Route


   Route::post('/upload', [FileController::class, 'uploadFile'])->name('uploadFile');
   Route::get('upload', [FileController::class, 'uploadIndex']);


the View

     @foreach ($files as $file)
                                            <tr>
                                               
                                                
                                                <td>{{ $file->filename }}</td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                                

                                            </tr>
                                            @endforeach 
                                        </tbody>
0 likes
15 replies
jlrdw's avatar

Why are you storing a file with one time and saving the name with another time.

1 like
Danny971's avatar

@jlrdw I used the time() function is used to generate a unique timestamp, which is appended to the original filename to ensure uniqueness. The stored file is then saved with this generated filename.

Similarly, the time() function is used again to generate a unique timestamp for the filename attribute of the fileModel.

I used this approach to help ensure that each file has a unique filename and can be easily identified.

Danny971's avatar

@jlrdw okay I will fix this but any idea why i get unidentified variable

Danny971's avatar

@jlrdw the problem is here

        $files = upload::where('family_id', $id)->get();
        dd($files);
        return view('profile')->with('files', $files);
    }

the dd($files); Is not even being executed
jlrdw's avatar

@Danny971 To display, try the asset helper.

<img src="{{ asset('upload/imgdogs') . '/' . $row->dogpic }}" alt="" class="image">

Use your path, just example.

1 like
mcpuishor's avatar

First problem I see is in the query to the model.

    $files = upload::where('family_id', $id)->get();

Where does that $id come from? Fix that issue first and then try

     return view('profile')->with(compact('files'));
1 like
Snapey's avatar

if dd is not called then you have confusion in your route naming. Nothing to do with files

Try stating the FULL error message or share the error page

1 like
Danny971's avatar

@Snapey I tried Something different but no luck


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\upload;

class ShowEvidenceController extends Controller
{
    

    public function ShowEvidence() {
        $files = upload:: all();
   
        return view('profile', ['docs' => $files]);
    }
}


  @foreach($docs as $doc)
                                            <tr>
                                                <td>{{ $doc['filename'] }}</td>
                                                <td></td>
                                                <td></td>
                                                <td></td>
                                            </tr>
                                         @endforeach


   Route::get ('/showEvidence',  [ShowEvidenceController::class, 'ShowEvidence']) ->name ('ShowEvidence');


where is the issue I still get  Undefined variable $docs
Danny971's avatar

I managed to solved it. Rather than using a new controller I had an exisiting controller that returns data to the profile view I just add the following code

and this in the return  'files' => $evidenceDocs,

Please or to participate in this conversation.