resultoffice's avatar

Route [cart.index] not defined.

PLEASE SOMEONE HELP ME, THE CODE ERROR IS : Route [cart.index] not defined.

THIS IS MY ROUTE ;

Route::get('/cart/', 'CartController@index')->name('cart.index');
Route::post('/cart/', 'CartController@store')->name('cart.store');

AND THIS IS MY CART CONTROLLER :

 public function store(Request $request)
    {
        
        Cart::add($request->id, $request->name, 1, $request->price)
            ->associate('App\Product');


            return redirect()->route('cart.index')->with('success_message', 'Item added to your cart!');


    }

MY ADD TO CARD FORM :

 <form class="cart clearfix" method="post" action="{{ route('cart.store') }}">
                                {{ csrf_field() }}
                                <input type="hidden"  name="id" value="{{$product->id}}">
                                <input type="hidden"  name="name" value="{{$product->name}}">
                                <input type="hidden"  name="price" value="{{$product->price}}">
                                <button type="submit" name="addtocart" value="5" class="btn amado-btn">Add to cart</button>
                            </form>

WARNING: IF YOUR ANSWER IS SO ABUSIVE I WILL GIVE IT BACK TO YOU IN FULL MEASURE. THANKS.

0 likes
13 replies
bobbybouwmann's avatar

REMOVE THE TRAILING / IN YOUR ROUTE!!!!!

Route::get('/cart', 'CartController@index')->name('cart.index');
Route::post('/cart', 'CartController@index')->name('cart.store');
1 like
bobbybouwmann's avatar

Do you see the route when you run php artisan route:list in your console?

resultoffice's avatar
+--------+----------+--------------------+----------------+-----------------------------------------------+--------------+
| Domain | Method   | URI                | Name           | Action                                        | Middleware   |
+--------+----------+--------------------+----------------+-----------------------------------------------+--------------+
|        | GET|HEAD | /                  | Home           | App\Http\Controllers\HomePageController@index | web          |
|        | GET|HEAD | api/user           |                | Closure                                       | api,auth:api |
|        | GET|HEAD | cart               |                | Closure                                       | web          |
|        | POST     | cart               | cart.store     | App\Http\Controllers\CartController@store     | web          |
|        | GET|HEAD | category           | category.index | App\Http\Controllers\CategoryController@index | web          |
|        | GET|HEAD | category/{product} | category.show  | App\Http\Controllers\CategoryController@show  | web          |
|        | GET|HEAD | checkout           |                | Closure                                       | web          |
|        | GET|HEAD | product            |                | Closure                                       | web          |
|        | GET|HEAD | thankyou           |                | Closure                                       | web          |
+--------+----------+--------------------+----------------+-----------------------------------------------+--------------+
bobbybouwmann's avatar

So it's not added to the routes! Where did you add that route? Is it in the web.php file? Or did you define it somewhere else?

resultoffice's avatar

@PopovMaxim didn't get a right answer yet and check before you say anything i have never accepted any answer as correct answer, you're good at picking out duplicate but never be in any part of solution.

PopovMaxim's avatar

@resultoffice, can i see, you solved a problem. The problem was precisely the duplication of the route. I'm glad that you solved your problem.

1 like
Arinze hills's avatar

in my own case is not because there are multiple routes but it is not saved when i saved it because it has already been stored in the route cache so when I run php artisan clear:cache it ran!

Please or to participate in this conversation.