slekniws
slekniws

Reputation: 139

Laravel 404 page not found, route exists

I am trying to navigate to my "productdetail" page but it givdes me a 404. The route to productdetail does exists. I am trying to give product information from shop to productdetail

My controller:

<?php

namespace App\Http\Controllers;

use DB;
use Illuminate\Http\Request;
use App\Product;

class ProductsController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function shopindex()
    {
        $productsOTs = DB::select(DB::raw("SELECT * FROM wiz.productimages WHERE Afkorting = 'PPI' LIMIT 83, 3"));
        return view('shop', compact('productsOTs'));
    }

    public function productdetail(Product $Product)
    {   
        return view('Products.productdetail', compact('productsOT'));

    }

}

My shop page link to productdetail:

           @foreach ($productsOTs as $productsOT)
                <div class="card ot-product" id="heightwidthfix">
                    <img class="card-img-top cardstop" src="{{$productsOT->imagelink}}" alt="Card image cap" id="myshopmodal1" height="400px" width="300px">
                    <div class="card-body">
                        <h5 class="card-title">{{$productsOT->Productomschrijving}}</h5>
                        <p class="card-text">Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
                    </div>
                    <div class="card-body">
                        <a href="/shop/productdetail/{{ $productsOT->Productcode }}" class="card-link">Bekijk hier het product</a>
                    </div>
                </div>
            @endforeach

My routes:

Route::get('/shop', ['middleware' => 'auth', 'uses' => 'ProductsController@shopindex']);
Route::get('/shop/productdetail/{product}', ['middleware' => 'auth', 'uses' => 'ProductsController@productdetail']);

How to resolve this problem?

Upvotes: 9

Views: 16572

Answers (11)

Naro
Naro

Reputation: 810

The problem is a little bit tricky:

What is happening :

Your Laravel application is currently using an out-dated routes cached file located in app/bootstrap/routes-x.php file instead of your routes files located in app/routes directory.

The solution :

  1. manualy delete app/bootstrap/routes-x.php file.

  2. in your app/routes (api.php or console.php ... ) routes files replace any route that is using a closure with controllerName@functionName

    example of route using a closure :

    Route::get('/route-using-closure', function() { return 'Hello World'; });

    Replace by:

    Route::get('/route-using-closure', 'controllerName@functionName');

  3. Only run php artisan route:cache in dev mode or any other caching command to avoid this errors.

Upvotes: 0

Mahmoud Ramadan
Mahmoud Ramadan

Reputation: 652

Solution 1

You are using the Route Binding feature but, not totally valid because the route segment and function parameter must be identical and this does not apply to what you did where your route segment is product and function parameter is Product, and this is invalid approach and when you pass the product id as a parameter, Laravel searches in the products table with the given id, and because the given id does not exist in the database Laravel trigger this page So, your code must be like so

web.php

Route::get('/shop/productdetail/{product}', ['middleware' => 'auth', 'uses' => 'ProductsController@productdetail']);

ProductsController.php

public function productdetail($product)
{   
    $products = \App\Models\Product::where("id", $product)->get();

    return view('Products.productdetail', compact('products'));
}

you may want to search with another column name than id so you must specify the other column name that you wish to search with as Laravel says here

Solution 2

If you register these routes in a new route file other than web.php, you must define this file in RouteServiceProvider.php as Laravel says here

Upvotes: 0

geeky
geeky

Reputation: 1652

Try following

<a href="url('/shop/productdetail', [$productsOT->Productcode])" class="card-link">Bekijk hier het product</a>

If this does not work, try

php artisan route:clear

If that does not work, try

php artisan optimize

Upvotes: 0

SHOAKROM KAMBAROV
SHOAKROM KAMBAROV

Reputation: 36

why are u doing $productsOt->ProductCode instead of $productsOt->id? If you need to get using the ProductCode. Use

Route::get('/shop/productdetail/{product:ProductCode}', ['middleware' => 'auth', 'uses' => 'ProductsController@productdetail']);

if your column name is ProductCode

Upvotes: 0

Morley
Morley

Reputation: 40

Hey buddy first thing to do this go to your command

  • php artisan route:clear
  • php artisan optimize

but I found something, I got worry in your code.

I prefer you 2 choices to solve this one

  • URL('/shop/productdetail/'.{{$id_here}})
  • or add router name in your routes. ->name('shop_productdetails); then use this in your blade {{route('shop_productdetails',$id)}}

You're Welcome buddy

Upvotes: 1

Junaed
Junaed

Reputation: 1957

I had issue with .htaccess. I uploaded, but it was somehow missing! I faced some other problems, everything is documented with my .htaccess file content here: https://stackoverflow.com/a/64828062/1938507

Upvotes: 0

Shailendra Gupta
Shailendra Gupta

Reputation: 1118

I think the problem with your route parameter that you have passed that is not appropriate pass

<a href="/shop/productdetail/{{ $productsOT->relevant_id_colunm_name }}" class="card-link">Bekijk hier het product</a>

Upvotes: 0

Inzamam Idrees
Inzamam Idrees

Reputation: 1981

Change this line:

<a href="/shop/productdetail/{{ $productsOT->Productcode }}" class="card-link">Bekijk hier het product</a>

into

<a href="{{ url('shop/productdetail/'.$productsOT->Productcode) }}" class="card-link">Bekijk hier het product</a>

This is the minor mistake. Change this, I hope it is helpful. Thanks

Upvotes: 1

Channaveer Hakari
Channaveer Hakari

Reputation: 2927

Please use the following way

Route::middleware(['auth'])->get('/shop/productdetail/{product}', 'ProductsController@productdetail');

I usually prefer to group the middleware so as to overcome writing it again and again by the following way

Route::group(['middleware' => 'auth'], function () {
        /** Dashboard */
        Route::match(['get', 'post'], '/dashboard', 'AdminController@dashboard');
        /**Customer */
        Route::get('/customers', 'AdminController@customerListing');
});

Upvotes: 0

Dearwolves
Dearwolves

Reputation: 453

Why don't you try to put a name for your route

Route::get('/shop/productdetail/{product}', ['middleware' => 'auth', 'uses' => 'ProductsController@productdetail'])->name('show-product');

then call it through it's name:

<a href="{{route('show-product',$productsOT->Productcode) }}" class="card-link">Bekijk hier het product</a>

This will might solve your problem otherwise the problem is not on the route.

Upvotes: 1

Mehran
Mehran

Reputation: 195

Please run this commend

php artisan route:cache

and also

php artisan cache:clear

Upvotes: 0

Related Questions