Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

404 error on route that exists in Laravel 8

I get a 404 error when I access this edit_product route that is served by a Product controller, codes are attached below Route::get('/edit_product{id}', 'ProductController@editproduct'); I would like your help in finding where the error in my code is. Thank you.

Here is the relevant code:

web.php

<?php

use IlluminateSupportFacadesRoute;

Route::get('/', 'ClientController@home');
Route::get('/checkout', 'ClientController@checkout');

Route::get('/shop', 'ClientController@shop');
Route::get('/cart', 'ClientController@cart');
Route::get('/login', 'ClientController@login');
Route::get('/signup', 'ClientController@signup');

Route::get('/dashboard', 'AdminController@dashboard');


Route::get('/addcategories', 'CategoryController@addcategories');
Route::post('/savecategories', 'CategoryController@savecategories');
Route::get('/categories', 'CategoryController@categories');
Route::get('/edit_category{id}', 'CategoryController@edit');
Route::get('/delete{id}', 'CategoryController@delete');


Route::get('/addslider', 'SliderController@addslider');
Route::get('/sliders', 'SliderController@slider');

Route::get('/addproducts', 'ProductController@addproducts');
Route::get('/products', 'ProductController@products');
Route::post('/saveproducts', 'ProductController@saveproduct');
Route::get('/edit_product{id}', 'ProductController@editproduct');

Route::get('/orders', 'ProductController@orders');

products.blade.php

@extends('admin.layouts.appadmin')
@section('title')
Products
@endsection
@section('content')
{{Form::hidden('', $increment=1)}}

<div class="card">
    <div class="card-body">
        <h4 class="card-title">Products table</h4>
        <div class="row">
            <div class="col-12">
                <div class="table-responsive">
                    <table id="order-listing" class="table">
                        <thead>
                            <tr>
                                
                                <th>Order #</th>
                                <th>Image</th>
                                <th>Name</th>
                                <th>Price</th>
                                <th>Category</th>
                                <th>Status</th>
                                <th>Actions</th>

                            </tr>
                        </thead>
                        @foreach ($products as $product)

                        <tbody>
                            <tr>
                                <td>{{$increment}}</td>
                                <td><img src="/storage/product_images/{{$product->product_image}}"></td>
                                <td>{{$product->product_name}}</td>
                                <td>KES{{$product->product_price}}</td>
                                <td>{{$product->product_category}}</td>
                                @if($product->status ==1)
                                <td>
                                    <label class="badge badge-success">Activated</label>
                                </td>
                                @else
                                <td>
                                    <label class="badge badge-danger">Un Activated</label>
                                </td>
                                @endif
                                <td>
                                    <button class="btn-sm btn-outline-info" onclick="window.location ='{{url('/edit_product/'.$product->id)}}'">Edit</button> 
                                    <a href="" class="btn-sm btn-outline-danger" id="delete">Delete</a>
                                    
                                    @if($product->status==1)
                                    <a href="" class="btn-sm btn-outline-warning">Unactivate</a>
                                    @else
                                    <a href="" class="btn-sm btn-outline-success">Activate</a>
                                    @endif
                                </td>
                            </tr>
                            {{Form::hidden('', $increment=$increment+1)}}
                            @endforeach
                            
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
@section('scripts')
<script src="{{'backend/js/data-table.js'}}"></script> 
@endsection

ProductController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use AppModelsProduct;
use AppModelsCategory;

class ProductController extends Controller
{
    public function editproduct($id){
        $product=Product::find($id);
        return view('admin.product')->with('product', $product);
    }

    public function products(){
        $products=Product::get();
        return view('admin.products')->with('products', $products);
    }


    public function orders(){
        return view('admin.orders');
    }

    public function addproducts(){
        $categories=Category::All()->pluck('category_name', 'category_name');

        return view('admin.addproducts')->with('categories', $categories);
    }

    public function saveproduct(Request $request){

        $this->validate($request, ['product_name'=> 'required',
                                    'product_price'=> 'required',
                                    'product_image'=>'image|nullable|max:1999']);

        if($request->input('product_category')){

            if($request->hasFile('product_image')){
            //1 : get filename with ext
                $fileNameWithExt = $request->file('product_image')->getClientOriginalName();

            //2 : get just file name
                $fileName = pathinfo($fileNameWithExt, PATHINFO_FILENAME);

            //3 : get just extension
                $extension = $request->file('product_image')->getClientOriginalExtension();

            //4 : file name to store


                $fileNameToStore = $fileName.'_'.time().'.'.$extension;

            //upload image
  
                $path =$request->file('product_image')->storeAs('public/product_images', $fileNameToStore);

            }

            else{  

                $fileNameToStore ='noimage.jpg';

            }

                    $product=new Product();
                    $product->product_name =$request->input('product_name');
                    $product->product_price =$request->input('product_price');
                    $product->product_category =$request->input('product_category');
                    $product->product_image =$fileNameToStore;
                    
                    $product->status =1;
                    $product->save();

                    return redirect('/addproducts')->with('status', 'The '.$product->product_name.' Product has been saved successfully');

        }else{
            return redirect('/addproducts')->with('status1', 'You need to select a Category for your product');

        }

        }
    }
 

I have tried.

  1. replacing it with full namespace: Route::get('/edit_product{id}', 'AppHttpControllersProductController@editproduct');
  2. php artisan cache:clear
  3. php artisan cache:cache
question from:https://stackoverflow.com/questions/65917589/404-error-on-route-that-exists-in-laravel-8

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Change your route to:

Route::get('/edit_product/{id}', 'ProductController@editproduct')

and also your delete and edit_category route:

Route::get('/delete/{id}', 'CategoryController@delete');

Route::get('/edit_category/{id}', 'CategoryController@edit');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...