9

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']);

I have been struggling with this problem for a while now, i hope someone can help me.

slekniws
  • 139
  • 1
  • 2
  • 6

11 Answers11

2

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

mahesmohan
  • 784
  • 1
  • 13
  • 33
Morley
  • 44
  • 3
1

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.

Dearwolves
  • 443
  • 4
  • 16
1

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

Inzamam Idrees
  • 1,955
  • 14
  • 28
  • Also remove middleware in your route because you add middleware in your controller constructor. And use simple route like Route::get('/shop/productdetail/{product}', 'ProductsController@productdetail'); – Inzamam Idrees Dec 11 '18 at 05:19
0

Please run this commend

php artisan route:cache

and also

php artisan cache:clear

Mehran
  • 175
  • 1
  • 10
0

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');
});
Channaveer Hakari
  • 2,769
  • 3
  • 34
  • 45
0

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>
Shailendra Gupta
  • 1,054
  • 6
  • 15
0

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

Junaed
  • 1,457
  • 13
  • 15
0

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

  • I recommend against rhetoric questions in answers. They risk being misunderstood as not an answer at all. You are trying to answer the question at the top of this page, aren't you? Otherwise please delete this post. – Yunnosch Jan 23 '22 at 13:53
  • Please phrase this as an explained conditional answer, in order to avoid the impression of asking a clarification question instead of answering (for which a comment should be used instead of an answer, compare https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead ). For example like "If your problem is ... then the solution is to .... because .... ." – Yunnosch Jan 23 '22 at 13:53
0

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
geeky
  • 1,592
  • 1
  • 8
  • 14
0

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

0

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.

Naro
  • 800
  • 6
  • 11