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?