0

I'm studying CURD with Laravel8

I use a sample index.blade.php This work really fine. so I'm trying to add new page which I made that is simple.blade.php.

I wrote below code but when I open http://127.0.0.1:8000/products/simple I got 404 NOT found error. Could you teach me right code please?

Controller

public function index()
{
    $products = Product::latest()->paginate(5);
  
    return view('products.index',compact('products'))
        ->with('i', (request()->input('page', 1) - 1) * 5);
}


public function simple()
{
    $products = Product::latest()->paginate(15);
  
    return view('products.simple',compact('products'))
        ->with('i', (request()->input('page', 1) - 1) * 5);
}

WEB.php

// This is index.blade.php
Route::resource('products', ProductController::class);

// This is my page which I added
Route::get('products.simple', [ProductController::class, 'simple']);

UPDATE I use this gentleman's awesome code

https://github.com/savanihd/Laravel-8-CRUD/blob/master/app/Http/Controllers/ProductController.php

UPDATE 2 error log

404
Not Found

     No query results for model [App\Models\Product] 
    simpleC:\xampp\htdocs\lara\lara-test\vendor\laravel\framework\src\Illuminate\Routing\ImplicitRouteBinding.php#47Illuminate\Database\Eloquent\ModelNotFoundException
    
throw (new ModelNotFoundException)->setModel(get_class($instance), [$parameterValue]);
 }
 } elseif (! $model = $instance->resolveRouteBinding($parameterValue, $route->bindingFieldFor($parameterName))) {
throw (new ModelNotFoundException)->setModel(get_class($instance), [$parameterValue]);
}
    
 $route->setParameter($parameterName, $model);
bluetail
  • 183
  • 8
  • 1
    Route::get('products/simple', [ProductController::class, 'simple']); – Ankita Dobariya Jun 29 '21 at 03:47
  • Thank you for answering me. I tried your code but I still got 404 error. – bluetail Jun 29 '21 at 03:52
  • this one happens because when you call products. simple then it's will find show method and you create a simple method so method not found public function simple() to rename public function show() and check it – Ankita Dobariya Jun 29 '21 at 04:11
  • if you want to check what's error occurred then make config true in env file and check it – Ankita Dobariya Jun 29 '21 at 04:12
  • Thank you again. Could you take a look my update that I use this CURD example code. my env is APP_DEBUG=true . and I'm sorry I renamed show function but I still god not found code. could you post the part of function code please? – bluetail Jun 29 '21 at 05:18
  • what's error occurs in your case? – Ankita Dobariya Jun 29 '21 at 05:28
  • When I hit http://127.0.0.1:8000/products/simple . 404 Not Found page show up. there is no debug message. – bluetail Jun 29 '21 at 05:33
  • show log file because this one perfectly works in my project – Ankita Dobariya Jun 29 '21 at 05:39
  • Sorry for late! I finally could debug. Here is my error log. show I post more below error code ? – bluetail Jun 29 '21 at 06:22
  • check data using var_dump or print_r() and check got it data in that method? – Ankita Dobariya Jun 29 '21 at 08:12
  • debug one by one first check route and then check-in controller method and check in view so you get it what's wrong in that method. – Ankita Dobariya Jun 29 '21 at 08:13
  • I'm very affraid to ask you that you said this simple.blade.php adding things works at your project right? if so Could you show me part of Controller code and web.php part please? This code is such a grouped wrting like Route::resource('products' ... I woule like to just display simple another index view as Route::get ... I've been doing this but now I got Invalid route action. I really don't know what to do next... – bluetail Jun 29 '21 at 08:55
  • 1
    If you list your routes with `php artisan route:list`, you should see that [the resource route](https://laravel.com/docs/8.x/controllers#actions-handled-by-resource-controller) you are using includes one like `GET products/{product}`, and that it is handled by `ProductController@show`. In that route, `{product}` is a wildcard, and will match anything - including `simple`. You will also see that that route appears *before* the route you set up for `products/simple` - so when you visit that URL, the *resource* route is handling it, not your `simple` route. – Don't Panic Jun 29 '21 at 09:45
  • 1
    The order of your routes matters - the first one that matches will be used. In your case, just move the simple route above the resource route. – Don't Panic Jun 29 '21 at 09:48
  • There are many examples/duplicates of this here, eg https://stackoverflow.com/questions/57169011/laravel-getting-404-error-when-creating-new-route/57169555, https://stackoverflow.com/a/62608419/6089612, https://stackoverflow.com/questions/61067602/laravel-6-search-route-giving-404, https://stackoverflow.com/questions/49998850/laravel-route-shows-404, ... – Don't Panic Jun 29 '21 at 09:53
  • Does this answer your question? [Laravel getting 404 error when creating new route](https://stackoverflow.com/questions/57169011/laravel-getting-404-error-when-creating-new-route) – Don't Panic Jun 29 '21 at 09:54
  • @Don't Panic Thank you very much! it worked! I learned a lot. Thank you! – bluetail Jun 30 '21 at 02:53
  • @Ankita Dobariya Thank you very much for keep eye on me. It worked now. – bluetail Jun 30 '21 at 02:54
  • @Don't Panic One thing "Laravel getting 404 error when creating new route" isn't my post. Thank you so much. – bluetail Jun 30 '21 at 02:55
  • Happy to help, and glad you've solved it. I am not sure what you mean about the duplicate I linked. [I am suggesting we close this question because it is a duplicate](https://stackoverflow.com/help/duplicates) of that one - the problem and answer in that question (and the others I listed) are the same as yours. – Don't Panic Jun 30 '21 at 07:33

0 Answers0