Index Method On Controller Is Called, But Not Used In Controller

- 1 answer

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



    <div class="row">
        <div class="col-sm-12">
            <h1 class="display-3">Stories</h1>
            <table class="table table-striped">
                    <td>Story ID</td>
                    <td>Author ID</td>
                    <td colspan = 2>Options</td>
                @foreach($stories as $story)
                            <a target="_blank" rel="nofollow noreferrer" href="{{ route('', $story->id)}}" class="btn btn-primary">View</a>
                            <a target="_blank" rel="nofollow noreferrer" href="{{ route('stories.edit', $story->id)}}" class="btn btn-primary">Edit</a>
                            <form action="{{ route('stories.destroy', $story->id)}}" method="post">
                                <button class="btn btn-danger" type="submit">Delete</button>

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', '[email protected]');
Route::get('stories/create', '[email protected]');
Route::get('stories/index', '[email protected]');

Any help would be greatly appreciated!



When you define the resource routes you dont need to define any other CRUD route, as said here

Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code


This single route declaration creates multiple routes to handle a variety of actions on the resource. The generated controller will already have methods stubbed for each of these actions, including notes informing you of the HTTP verbs and URIs they handle.

My guess is that this two routes are probably colliding with each other

Route::resource('stories', 'StoryController'); Route::get('stories/index', '[email protected]');

So you don't need this following routes:

Route::post('/stories', '[email protected]'); Route::get('stories/create', '[email protected]'); Route::get('stories/index', '[email protected]');

Also, make sure you are redirecting to the right view in your directory. and if the path if correct