resultoffice's avatar

Route [cart.index] not defined ERROR

I HAVE CHECK THE ROUTE BUT NOT IN ROUTE:LIST 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
18 replies
rin4ik's avatar

please run php artisan route:list and make sure you have that named route

rin4ik's avatar

What is not included ? try to run php artisan route:clear and check again

1 like
resultoffice's avatar

@rin4ik i just install the laravel yesterday and that means is the latest fashion i run php artisan route:clear as you said and then i run php artisan route:list i only have the following route : home, cart.store, category.index and category.show

but cart.index is not there.

Snapey's avatar

What file did you put the route statements in?

36864's avatar

Post your full web.php file and the full output of php artisan route:list.

Tray2's avatar

Yes and please make sure the large button to the left of the a-key is not active.

2 likes
Nakash's avatar

He's probably talking about the caps locks key :)

In order for us to help you, you would have to post the entire content of your web.php file framed by three " ` ", and same thing for the output of your php artisan route:list

resultoffice's avatar

My web.php


/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', 'HomePageController@index')->name('Home');

Route::get('/category', 'CategoryController@index')->name('category.index');


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


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





Route::get('/category/{product}', 'CategoryController@show')->name('category.show');

// Route::get('/category', function () {
//     return view('category');
// });
Route::get('/product ', function () {
    return view('product ');
});
Route::get('/checkout', function () {
    return view('checkout');
});
Route::get('/cart', function () {
    return view('cart');
});
Route::get('/thankyou', function () {
    return view('thankyou');
});



php artisan route:list

+--------+----------+--------------------+----------------+-----------------------------------------------+--------------+
| 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          |
+--------+----------+--------------------+----------------+-----------------------------------------------+--------------+


Snapey's avatar

this

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

and

Route::get('/cart', function () {
    return view('cart');
});

are the same route. The second probably taking precedence and causing the named route to be lost

2 likes
resultoffice's avatar

@Snapey same error after using

Route::get('/cart', function () {
    return view('cart');
});
36864's avatar

You were supposed to remove that part and keep the named route.

What you were basically doing is defining a route on /cart/ and saying that route is named cart.index. Then you're defining a route again on /cart/ but not giving it a name at all. You can't have two routes on the same path, so whatever route is defined last will overwrite the previous route.

In simple pseudo-code terms, this is what you were doing:

route['cart'] = new Route('cart.index');
route['cart'] = new Route();

Hopefully that will help you in understanding your mistake.

m7vm7v's avatar
m7vm7v
Best Answer
Level 51

@resultoffice just remove the second route and the problem will be solved.

REMOVE -

Route::get('/cart', function () {
    return view('cart');
});

then in your CartController@index do the magic you want such as return view('cart');

1 like

Please or to participate in this conversation.