Error 404 Not found Im trying to reach the edit page of sales, but it display 404 not found. But the rest of the routes are working so well, except for this one.
route
Route::resource('/sales', 'SaleController');
index.blade.php
{{--Display name--}}
<td><a href="{{route('sales.edit', $sale)}}">{{$sale->name}}</a></td>
controller
public function edit(Sale $sales)
{
$id = $sales->id;
$sales = Sale::findOrFail($id);
return view ('sales.edit', compact ('sales'));
}
this is the position of the blade
https://imgur.com/a/dy6y7xv
$id = $sales->id;
$sales = Sale::findOrFail($id);
Not quite sure why you are getting the sale from the database again if you got the id from a Sale object right before that :D
I don't usually use resource controllers but maybe the / in '/sales' is the problem?
The view isn't the problem. With a 404 it's one of the following:
Your URL cannot be found
The ID of the sale is not in the database (like you are calling /sales/10 but ID 10 doesn't exist)
Again the ID of the sale is not in the database when you use findOrFail. Both do the same thing so you don't need that.
One more tip regarding the resource controller. In your controller's edit function you pass through the model with route-model binding (Sale $sales) but i think it should be $sale (singular). That is the way or pattern Laravel generates the routes. But just to be sure, check your routes with the
php artisan route:list
command and look for that edit route, similar to this:
GET|HEAD | sales/{sale}/edit | sales.edit | App\Http\Controllers\SaleController@edit
The value between the {} chars has to be the same you pass in the controller.
change the controller:
public function edit(Sale $sale)
{
return view ('sales.edit', compact ('sale'));
}
and then set the view up to expect $sale not $sales
Please sign in or create an account to participate in this conversation.