How to delete data from database in Laravel 8.*

Viewed 6694

I'm trying to delete city from my db but i really got no clue what to do next. Since I didnt find anything about deleteing data from db for Laravel 8.* I think this might be usefull for other stackoverflowers too.

My error says clearly that I'm missing some arguments,

Too few arguments to function App\Http\Controllers\CityController::delete(), 0 passed in C:\xampp\htdocs\Laravel1\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 1 expected

While my controller says that he expected type 'object' but only found array

Exoected type 'object'. Found 'array'. intelephense(1006)
@var array $todelete

Here is Route from web.php

Route::delete('cities', [App\Http\Controllers\CityController::class, 'delete'])->name('cities.delete');

And this is form I'm using to delete

<form class="float-right m-0" method="post" action="{{ route('cities.delete') }}">
                                @method('delete')
                                @csrf
                                <div class="form-row">
                                    <input type="hidden" name="cityId" value="{{ $city->id }}">
                                    <button> <i class="fa fa-trash-alt"></i></button>
                                </div>
                            </form>

Edit

After solving previous problems, I'm stuck on another error:

Call to a member function delete() on array 

Here is how my "CityController"s function looks like after updates

public function delete(Request $request)
    {
        $cityId = $request->get('cityId', false);
    // TODO: Check for validation
    $todelte = DB::select('select '.$cityId.' from cities');
    $todelte -> delete();
    return redirect('frontend/city/index');
    }

Correct query to delete stuff: DB::table('cities')->where('id', $cityId)-> delete();

1 Answers

The issue with not getting the $id parameter passed has to do with your route not accepting any parameters. Your call is DELETE /cities, which does have any parameters. If you changed that to DELETE /cities/5, than you could get a parameter passed down the line.

In order to get the parameter to your controller you should change your route as follows:

Route::delete('cities/{id}', [App\Http\Controllers\CityController::class, 'delete'])->name('cities.delete');

This way the {id} part will be passed along to the function specified in the callback. To make your form work with that new route you should change the action-part of you form to direct to the new URL. I suggest using the 2nd parameter as well so you get:

route('cities.delete', ['id' => $city->id])

Then you can remove the hidden input.

If you do want to keep the existing form you need to use the Request as a parameter in your delete-function. That would look like this:

public function delete(\Illuminate\Http\Request $request)
{
    $cityId = $request->get('cityId', false);
    // TODO: Check for validation
    $todelte = DB::select('select '.$cityId.' from cities');
    $todelte -> delete();
    return redirect('frontend/city/index');
}

Related