I'm working on a Laravel project, and I am running into a problem where I can't get my index page to work without throwing a 404 error.
I'm working on an amateur authoring website, which has a database of stories - the user can either upload a text or file etc. I have an index page that displays all stories in a long table. Here is the page and the controller method for calling it:
Controller (showing only relevant method):
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$stories = Story::latest()->get();
return view('stories.index', compact('stories'));
}
index.blade.php:
@extends('base')
@section('main')
<div class="row">
<div class="col-sm-12">
<h1 class="display-3">Stories</h1>
<table class="table table-striped">
<thead>
<tr>
<td>Story ID</td>
<td>Title</td>
<td>Author ID</td>
<td>Genre</td>
<td colspan = 2>Options</td>
</tr>
</thead>
<tbody>
@foreach($stories as $story)
<tr>
<td>{{$story->id}}</td>
<td>{{$story->title}}</td>
<td>{{$story->authorid}}</td>
<td>{{$story->genre1}}</td>
<td>
<a href="{{ route('stories.show', $story->id)}}" class="btn btn-primary">View</a>
</td>
<td>
<a href="{{ route('stories.edit', $story->id)}}" class="btn btn-primary">Edit</a>
</td>
<td>
<form action="{{ route('stories.destroy', $story->id)}}" method="post">
@csrf
@method('DELETE')
<button class="btn btn-danger" type="submit">Delete</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
@endsection
When I try to access this page, I keep getting a 404 error. I know that I was previously able to access it because it was throwing other errors related to the database, but when these were fixed I got the 404. I have no idea where I could be going wrong here. It also says in the controller method definition that the element is not used - why it says this I don't know, as it is clearly referenced from the routes file:
Route::get('/', function () {
return view('welcome');
});
Route::get('/soon', function () {
return view('soon');
});
Route::resource('stories', 'StoryController');
Route::post('/stories', 'StoryController@store');
Route::get('stories/create', 'StoryController@create');
Route::get('stories/index', 'StoryController@index');
Any help would be greatly appreciated!