Skip to content
Advertisement

Route [admin.editcategory] not defined

Hello Everyone I am new in laravel. I just started to watch a series of ecommerce from youtube. In the series at the 15th video , when editing the category i did as same as the youtuber did. But there is an error. Please help me anyone.

SymfonyComponentRoutingExceptionRouteNotFoundException  
Route [admin.editcategory] not defined. 
(View: C:xampphtdocsLaravelXencommerceresourcesviewslivewireadminadmin-category-component.blade.php)

Here is my web.php codes

<?php

use AppHttpLivewireHomeComponent;
use AppHttpLivewireShopComponent;
use AppHttpLivewireCartComponent;
use AppHttpLivewireCheckoutComponent;
use AppHttpLivewireContactComponent;
use AppHttpLivewireAboutComponent;
use AppHttpLivewireDetailsComponent;
use AppHttpLivewireCategoryComponent;
use AppHttpLivewireHeaderSearchComponent;
use AppHttpLivewireSearchComponent;
use AppHttpLivewireUserUserDashboardComponent;
use AppHttpLivewireAdminAdminDashboardComponent;
use AppHttpLivewireAdminAdminCategoryComponent;
use AppHttpLivewireAdminAdminAddCategoryComponent;
use AppHttpLivewireAdminAdminEditCategoryComponent;
use IlluminateSupportFacadesRoute;

/*
|--------------------------------------------------------------------------
| 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('/', function () {
    return view('welcome');
});
 Route::get('/',HomeComponent::class);
 Route::get('/shop',ShopComponent::class);
 Route::get('/cart',CartComponent::class)->name('product.cart');
 Route::get('/checkout',CheckoutComponent::class);
 Route::get('/contact',ContactComponent::class);
 Route::get('/about',AboutComponent::class);
 Route::get('/product/{slug}',DetailsComponent::class)->name('product.details');
 Route::get('/product-category/{category_slug}',CategoryComponent::class)->name('product.category');
 Route::get('/search',SearchComponent::class)->name('product.search');
// Route::middleware(['auth:sanctum', 'verified'])->get('/dashboard', function () {
//     return view('dashboard');
// })->name('dashboard');


// For User and Customer
Route::middleware(['auth:sanctum','verified'])->group(function(){
    Route::get('/user/dashboard',UserDashboardComponent::class)->name('user.dashboard');
});

// For Admin
Route::middleware(['auth:sanctum','verified','authadmin'])->group(function(){
    Route::get('/admin/dashboard',AdminDashboardComponent::class)->name('admin.dashboard');
    Route::get('/admin/categories',AdminCategoryComponent::class)->name('admin.categories');
    Route::get('/admin/categories/add',AdminAddCategoryComponent::class)->name('admin.addcategory');
    Route::get('/admin/categories/edit/{category_slug}',AdminEditCategoryComponent::class)->name('admin.editcategory');
});

Here is my AdminCategoryComponent.php codes

<?php

namespace AppHttpLivewireAdmin;

use AppModelsCategory;
use LivewireComponent;
use LivewireWithPagination;

class AdminCategoryComponent extends Component
{
    use WithPagination;
    public function render()
    {
        $categories = Category::paginate(5);
        return view('livewire.admin.admin-category-component',['categories'=>$categories])->layout('layouts.base');
    }
}
Here is admin-category-component.blade.php

<div>
    <style>
        nav svg{
            height: 20px;
        }
        nav .hidden{
            display: block !important;
        }
    </style>
    <div class="container" style="padding: 30px 0;">
        <div class="row">
            <div class="col-md-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <div class="row">
                            <div class="col-md-6">
                                All Categories
                            </div>
                            <div class="col-md-6">
                                <a href="{{route('admin.addcategory')}}" class="btn btn-success pull-right">Add New Category</a>
                            </div>
                        </div>
                    </div>
                    <div class="panel-body">
                        <table class="table table-striped">
                            <thead>
                                <tr>
                                    <th>Id</th>
                                    <th>Category Name</th>
                                    <th>Slug</th>
                                    <th>Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach ($categories as $category)
                                <tr>
                                    <th>{{$category->id}}</th>
                                    <th>{{$category->name}}</th>
                                    <th>{{$category->slug}}</th>
                                    <th>
                                        <a href="{{ route('admin.editcategory',['category_slug'=>$category->slug]) }}"><i class="fa fa-edit fa-2x"></i></a>                        
                                        <!-- <a href="{{route('admin.deletecategory')}}"><i class="fa fa-trash fa-2x"></i></a>                        -->
                                    </th>
                                </tr>
                                @endforeach
                            </tbody>
                        </table>
                        {{$categories->links()}}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Here is AdminEditCategoryComponent.php

<?php

namespace AppHttpLivewireAdmin;

use AppModelsCategory;
use LivewireComponent;
use IlluminateSupportStr;

class AdminEditCategoryComponent extends Component
{
    public $category_slug;
    public $category_id;
    public $name;
    public $slug;
    public function mount($category_slug)
    {
        $this->category_slug = $category_slug;
        $category = Category::where('slug',$category_slug)->first();
        $this->category_id = $category->id;
        $this->name = $category->name;
        $this->slug = $category->slug;
    }

    public function generateslug()
    {
        $this->slug = Str::slug($this->name);
    }

    public function updateCategory()
    {
        $category = Category::find($this->category_id);
        $category->name = $this->name;
        $category->slug = $this->slug;
        $category->save();
        session()->flash('message','Your category has beed updated successfully');
    }

    public function render()
    {
        return view('livewire.admin.admin-edit-category-component')->layout('layouts.base');
    }
}

Here is my admin-edit-category-component.blade.php

<div>
    <div class="container" style="padding: 30px 0;">
        <div class="row">
            <div class="col-md-12">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <div class="row">
                            <div class="col-md-6">
                                Edit Category
                            </div>
                            <div class="col-md-6">
                                <a href="{{route('admin.categories')}}" class="btn btn-success pull-right">All Categories</a>
                            </div>
                        </div>
                    </div>
                    <div class="panel-body">
                        @if(Session::has('message'))
                         <div class="alert alert-success" role="alert">{{Session::get('message')}}</div>
                        @endif
                        <form class="form-horizontal" wire:submit.prevent="updateCategory">
                            <div class="form-group">
                                <label class="col-md-4 control-label">Category Name</label>
                                <div class="col-md-4">
                                    <input type="text" placeholder="Category Name" class="form-control input-md" wire:model="name" wire:keyup="generateslug">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-md-4 control-label">Category Slug</label>
                                <div class="col-md-4">
                                    <input type="text" placeholder="Category Slug" name="" class="form-control input-md" wire:model="slug">
                                </div>
                            </div>

                            <div class="form-group">
                                <label class="col-md-4 control-label"></label>
                                <div class="col-md-4">
                                    <button type="submit" class="btn btn-primary">Update Category</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

Advertisement

Answer

Change the href in admin-category-component.blade.php to href=”‘/admin/categories/edit/{{$category->slug}}” and remove the route name . And it should be fine.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement