Home > Net >  Send Parameter to Controller in Routes Laravel
Send Parameter to Controller in Routes Laravel

Time:10-24

I got error that says

Method App\Http\Controllers\Frontend\Atribut\AtributDashboardController::deleteData/{id} does not exist.

I write my routes like this :

Route::group([
  'prefix' => 'atribut',
  'as' => 'atribut.'
], function () {
  
  Route::group(['prefix' => 'tabHome', 'as' => 'tabHome.'], function () {
    Route::get('', [AtributDashboardController::class, 'showTab'])->name('showTab');
    Route::post('', [AtributDashboardController::class, 'addData'])->name('addData');
    Route::get('', [AtributDashboardController::class, 'deleteData/{id}'])->name('deleteData');
    
  });

in the controller i also already write these :

public function deleteData($id)
{
    $this->inpData->deleteData($id);
    return redirect('atribut/tabHome');
}

i call $this->inpData->deleteData($id) from my models :

public function deleteData($id)
{
    DB::table('inp_datas')->where('id', $id)->delete();
}

this is the button action when delete button pressed:

 @forelse ($dataDisplay as $data)
  <tr>
   <td>{{$data->name}}</td>
   <td>
     <a href="{{route('frontend.atribut.tabHome.deleteData', $data->id)}}" class="btn btn-sm btn-danger">Delete</a>
   </td>
  </tr>
 @empty
 @endforelse

why it says the method does not exist?

CodePudding user response:

As you can see in the documentation, the Route first argument is route name, and in the array you have to specify class and its method.

Why it says the method does not exist?

In your AttributDashboardController you have deleteData method, not deleteData/{id}. The deleteData/{id} is more likely to be a route name in your case.

So for your example, delete route should look like this:

Route::get('deleteData/{id}', [AtributDashboardController::class, 'deleteData'])->name('deleteData');

or:

Route::get('{id}', [AtributDashboardController::class, 'deleteData'])->name('deleteData');

Laravel is smart enough to pass the {id} parameter to your deleteData method as $id argument.

HTTP DELETE

Also, if you want to make delete at this route, you will probably benefit from reading about available router methods, and especially HTTP DELETE.

  • Related