joe m
joe m

Reputation: 85

Laravel not navigating to url based on database id

I am attempting to navigate to a product page with laravel. The URL gets an id which is obtained from the database id which it uses to display the product.

Products controller file:

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

   public function show($id)
    {
        $product = Product::find($id);
        return view('products.show')->with('product',$product);
    }

Error:

The requested URL /products/1 was not found on this server.

Here's my routes as requested:

<?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('/', function () {
    return view('HomePage');
});

Route::get('/about', function () {
    return view('pages.about');
});


   /*
     * Login
     */
    Route::get('/login', [
        'as'   => 'login',
        'uses' => 'UserLoginController@showLogin',
    ]);
    Route::post('/login', 'UserLoginController@postLogin');

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');



Route::resource('products','productcontroller');

Upvotes: 2

Views: 127

Answers (3)

joe m
joe m

Reputation: 85

the href tag was wrong and needed to be exactly what was in the localhost e.g. localhost/8888/public

Upvotes: 0

Mahmut D.
Mahmut D.

Reputation: 36

Edit: is your controller class named correctly? I think issue is there. Yes you can use resource route for restful request response but route can't find your controller class. Name it correctly. ProductsController or something. Double check.

Can you paste your route? Problem is probably there. Not in your controller. Check the documentation, if you can't figure it out paste here.

Upvotes: 1

Jack jdeoel
Jack jdeoel

Reputation: 4584

You don't have any route like this /products/1 so need to add that route and assign the controller and action that you need . Eg.

Route::get('/product/{id}', 'ProductController@show')

Upvotes: 1

Related Questions