Ad

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

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 target="_blank" rel="nofollow noreferrer" href="{{ route('stories.show', $story->id)}}" class="btn btn-primary">View</a>
                        </td>
                        <td>
                            <a target="_blank" rel="nofollow noreferrer" 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', '[email protected]');
Route::get('stories/create', '[email protected]');
Route::get('stories/index', '[email protected]');

Any help would be greatly appreciated!

Ad

Answer

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

Ad
source: stackoverflow.com
Ad