Problem accessing a route that has no parameter in Laravel 8

89 Views Asked by At

I have a problem accessing my route, I have a "pedidos" view where it contains some information, features and links. In one of these links I have a href with the route "pedidos.imports", every time I click on this link the route gives me an error. The route has already been created, the view too and I have the function in my controller. The only solution I found was to change the order of the routes, but that doesn't seem like the best thing to do. It could be a trivial error, but I can't solve it, even though I've already worked with several errors throughout my project.

Here's the error:

App\Http\Controllers\ReserveController::show(): Argument #1 ($id) must be of type int, string given, called in
C:\xampp\htdocs\ApLabRequest\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54

Link:

<a style="font-size: 20px;" href="{{route('pedidos.imports')}}" class="btn btn-info form-control">
 <i class="fas fa-upload"></i>
 Importar
</a>

Route:

Route::resource('pedidos', ReserveController::class)->middleware('auth');
Route::get('pedidos/imports', [ReserveController::class, 'imports'])->name('pedidos.imports')->middleware('auth');

Controller:

public function imports()
{
    $deeps = Deep::get();
    $clients = Client::with('properties')->orderBy('name')->get();
    $kinds = Kind::get();
    $materials = Material::get();
    $cultures = Culture::get();
    $paymentMethods = PaymentMethod::get();
    return view('reserves.imports', compact('deeps', 'clients', 'kinds', 'materials', 'cultures', 'paymentMethods'));
}

"Solution" I found:

Route::get('pedidos/imports', [ReserveController::class, 'imports'])->name('pedidos.imports')->middleware('auth');
Route::resource('pedidos', ReserveController::class)->middleware('auth');

One thing I realized is that all the routes I created so far had some parameter, but in this new route I don't need a parameter and it doesn't even make sense to have it (for me). Summarizing what I'm trying to implement, this route returns a view where I will import an excel file and save these values in the database.

0

There are 0 best solutions below