Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jwavess's avatar

How do I properly use route::"resource" with my project?

When I write my routes out by hand everything runs smoothly, when I try and use "resource" instead I get this error.

NotFoundHttpException in RouteCollection.php line 16

Here is my hand written routes, these routes make everything run smoothly, why will resource not work.

Route::get('food/create', 'FoodController@create');

Route::post('food/post', 'FoodController@store');

Route::get('food/{id}', 'FoodController@index');

Below is the rest of my code from the other files, and if you would like for my to show you what "php artisan route:list" brings up when I hand write the routes and when "resource" writes them, I would be more than happy to show you in a comment.

(In the controller file)

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Controllers\Controller;

use App\Food;

use App\Http\Requests\CreateFoodRequest;

class FoodController extends Controller
{



    public function index($id) {

        $food = Food::find($id);

        return view('index')->with('food', $food);

    }

    public function create() {

        return view('vendor.create');
    }


    public function store(CreateFoodRequest $request) {

        $input = $request->all();

        $food = Food::create($input);

        return redirect('food/'.$food->id);
    }



}

(in the index file)

@extends('app')



@section('body')

<h1>Foods!</h1>


<a href ="food/{!! $food->id !!}"><h2>{{ $food->vegetables }}</h2></a>
<a href ="food/{!! $food->id !!}"><h2>{{ $food->fruit }}</h2></a>
<a href ="food/{!! $food->id !!}"><h2>{{ $food->grains }}</h2></a>
<a href ="food/{!! $food->id !!}"><h2>{{ $food->meat }}</h2></a>
<a href ="food/{!! $food->id !!}"><h2>{{ $food->sugar }}</h2></a>

@stop('body')

(In the create file)

@extends('app')

@section('body')



    {!! Form::open(['url'=>'food/post']) !!}




    

<div class="form-group">


        {!! Form::label('vegetables', 'Vegetable item:') !!}



        {!! Form::text('vegetables', null, ['class' => 'form-control']) !!}


    </div>





    

<div class="form-group">


        {!! Form::label('fruit', 'Fruit item:') !!}



        {!! Form::text('fruit', null, ['class' => 'form-control']) !!}


    </div>







    <div class="form-group">


        {!! Form::label('meat', 'Meat item:') !!}



        {!! Form::text('meat', null, ['class' => 'form-control']) !!}

    </div>



    

<div class="form-group">


            {!! Form::label('grains', 'Grain item:') !!}

        

    {!! Form::text('grains', null, ['class' => 'form-control']) !!}
        
</div>






    <div class="form-group">


            {!! Form::label('sugar', 'Sugar item:') !!}

        

    {!! Form::text('sugar', null, ['class' => 'form-control']) !!}

    </div>




    

<div>

{!! Form::submit('Add Food items', ['class' => 'btn btn-primary form-control']) !!}






        {!! Form::close() !!}

</div>




        @if($errors->any())
            <ul class="alert alert-danger">

                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>

                @endforeach

            </ul>
        @endif


@stop('body')

Seth

0 likes
11 replies
Hamelraj's avatar

If you use variable in index method like this

 public function index($id) {}

you cant use resource routes, if you want use variable manually just do it

Route::get('foods/{id}', 'FoodController@index');

in here you not set routes for index method thats the reason you will get error on that line or better you can use @zachleigh idea to use show method

jwavess's avatar

@Hamelraj @zachleigh Ill try and change it to what you two mentioned, for now, when I type

php artisan route:list

I get this, when I am using resource.

| GET|HEAD | food             | food.index   | App\Http\Controllers\FoodController@index   |            |
|        | POST     | food             | food.store   | App\Http\Controllers\FoodController@store   |            |
|        | GET|HEAD | food/create      | food.create  | App\Http\Controllers\FoodController@create  |            |
|        | DELETE   | food/{food}      | food.destroy | App\Http\Controllers\FoodController@destroy |            |
|        | PATCH    | food/{food}      |              | App\Http\Controllers\FoodController@update  |            |
|        | GET|HEAD | food/{food}      | food.show    | App\Http\Controllers\FoodController@show    |            |
|        | PUT      | food/{food}      | food.update  | App\Http\Controllers\FoodController@update  |            |
|        | GET|HEAD | food/{food}/edit | food.edit    | App\Http\Controllers\FoodController@edit    |            |
jwavess's avatar

@Hamelraj @zachleigh I just tried changing the "index" to "show" in "FoodController", and I received this error.

MethodNotAllowedHttpException in RouteCollection.php line 219:

By the way, the resource will work when I go to food/create. The error occurs once I type in some random values and expect to be redirected. THATS when I am getting the error.

Romain's avatar

When you type random values where? In the URL bar in your browser? In the form to create food?

It's normal to get an error if you type random stuff in your browser navigation bar, coz that URL won't be found.

When you register a resource, as the php artisan route:list command shows, to access 1 food, you need to go to 'food/1' using the GET method (that's just hitting the URL: mysite.com/food/1). 1 being the ID = 1. That will use the method 'show' on 'FoodController' which receives the $id. That's why @zachleigh told you to switch 'index' for 'show'.

Hope that helps

zachleigh's avatar
Level 47

Your form is posting to 'food/post' but when using a resource controller, that route doesn't exist. So, Laravel assumes that 'post' is a variable and is trying to hit food/{food}. However, this route does not allow post requests. That is why you are getting this error.
When using resource controllers, you have to follow conventions. Your form should be posting to 'food'.

4 likes
Hamelraj's avatar

i think now using resource routes ryt then try this

{!! Form::open(['route'=>['food.store']]) !!}
jwavess's avatar

@zachleigh so instead of having

  {!! Form::open(['url'=>'food/post']) !!}

I need to have

  {!! Form::open(['url'=>'food']) !!}

?

ALSO if I wanted it to go to the food/{id}, like in my redirect, could i do something like this

  {!! Form::open(['url'=>'"'food/'.$food->id]) !!}

And dose this mean I can switch show back to index?

Seth

zachleigh's avatar

Yes, that should work. What @Hamelraj suggested should also work. Check the docs for the html/forms package for more info. https://laravelcollective.com/docs/5.0/html#opening-a-form

As you're using your form, you can not send it to ['url'=>'"'food/'.$food->id] because that route can't take a post request. And no, changing your controller method to index isn't going to work. Check the docs for more about resource controllers. https://laravel.com/docs/5.1/controllers#restful-resource-controllers

1 like

Please or to participate in this conversation.