gdavies4's avatar

Getting an undefined variable error, not sure why

So I'm trying to build an app wherein a user can upload a file, view files and then download files as well. I've been following a tutorial at https://www.codetutorial.io/laravel-5-file-upload-storage-download/

So, I have my routes set up like so:

<?php

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index', 'FileEntryControlled@index');

Route::get('fileentry', 'FileEntryController@index');
Route::get('fileentry/get/{filename}', [
    'as' => 'getentry', 'uses' => 'FileEntryController@get']);
Route::post('fileentry/add',[
    'as' => 'addentry', 'uses' => 'FileEntryController@add']);

And my FileEntryController like so:

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Fileentry;
use Request;

use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Http\Response;

class FileEntryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $entries = Fileentry::all();
        return $entries;
        return view('fileentries.index', compact('entries'));
    }

    public function add() {

        $file = Request::file('filefield');
        $extension = $file->getClientOriginalExtension();
        Storage::disk('local')->put($file->getFilename().'.'.$extension,  File::get($file));
        $entry = new Fileentry();
        $entry->owner = auth()->id();
        $entry->mime = $file->getClientMimeType();
        $entry->original_filename = $file->getClientOriginalName();
        $entry->filename = $file->getFilename().'.'.$extension;

        $entry->save();

        return redirect('fileentry');

    }

    public function get($filename){

        $entry = Fileentry::where('filename', '=', $filename)->firstOrFail();
        $file = Storage::disk('local')->get($entry->filename);

        return (new Response($file, 200))
            ->header('Content-Type', $entry->mime);
    }
}

Finally, the page where the problem is happening is my home.blade.php:

@extends('app')
@section('content')
<div class="container">
    <div class="row">
        <div class="col-md-8 col-md-offset-2">
            <div class="panel panel-default">
                <div class="panel-heading">Dashboard</div>

                <div class="panel-body">
                    You are logged in!

                    <form action="add" method="post" enctype="multipart/form-data">
                        <input type="file" name="filefield">
                        <input type="submit">
                    </form>

                    <h1> File list </h1>

                    <div class="row">

                        <ul>
                            @foreach($entries as $entry)
                                <li>{{$entry->filename}}</li>
                            @endforeach
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

So my problem is that when trying to access my homepage, I'm getting the error: Undefined variable: entries (View: C:\laragon\www\kusnow\resources\views\home.blade.php). Could someone tell me why this is happening, or what I've done wrong?

0 likes
7 replies
Snapey's avatar

assume you mean GET /home...

Not sure what that third parameter is doing, but clearly in your home.blade.php if you use @foreach(entries then you better be sure it exists.

You don't show HomeController so I don't know how if it is trying to initialise $entries

gdavies4's avatar

My home controller is as follows:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('home');
    }
}

Sorry if it's something very obvious, I've followed the Laracast lessons but still do not really understand how everything comes together.

harrymessenger's avatar

Hi @gdavies4

There's a typo in the route you're calling "d" instead of "r" at the end of the line, also there should only be one controller method per route, either IndexController or FileEntryController not both

gdavies4's avatar

I have corrected the typo and also taken out the HomeControllerIndex. The code in my web.php now looks like:

<?php

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', function(){
  return view('home');
});

Route::get('fileentry', 'FileEntryController@index');
Route::get('fileentry/get/{filename}', [
    'as' => 'getentry', 'uses' => 'FileEntryController@get']);
Route::post('fileentry/add',[
    'as' => 'addentry', 'uses' => 'FileEntryController@add']);
Snapey's avatar

So you cannot @foreach($entries in your home controller as you have not given it $entries.

In the home controller index method, you need to initialise entries and pass it to the view.

harrymessenger's avatar

The other problem was that there are two return statements in fileentrycontroller/index. Removing the first should fix this

1 like
gdavies4's avatar

Have followed your advice and it is now (almost) working, thank you!

Now I just have a problem uploading files...

Thanks very much!

Please or to participate in this conversation.