1

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 Answer 1

2

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');
}

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for the response, I knew it before and I tried already with your method earlier but I finished with different type of bug: Call to a member function delete() on array. It wasn't showed as a bug before but I had it inside controller (mentioned in second code quote)
Also, url after those chances looks kinda funny: 127.0.0.1:8000/cities/%24id?id=29
That is because you're printing the route in {{ }}-tags, those performl HTML-encoding over your URL. If you change those to {!! !!} then is should be printed without encoding.
I was just dummie and i did 'id' instead of {id}. Now url looks legit, however i still cant do anything because of the error: Call to a member function delete() on array.
Well that's a whole new issue, you're selecting data from the database and trying to call ->delete() on that, but the DB::select most likely returns an array. You should change you query to a delete-query or use Eloquent to manage these things, but that requires some more rewriting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.