How to solve Missing argument 2 for App\Http\Controllers\UserController::destroy() in laravel 5.3?

2.4k views Asked by At

My view is like this :

@foreach($users as $user)
    <tr>
        <td>{!! $user->id !!}</td>
        <td>{!! $user->username !!}</td>
        <td>{!! $user->phone !!}</td>
        <td>{!! $user->address !!}</td>
        <td>
            {!! Form::open(['route' => ['users.destroy', $user->id], 'method' => 'delete']) !!}
            <div class='btn-group'>
                <a href="{!! route('users.edit', [$user->id]) !!}" class='btn btn-default btn-xs'><i class="glyphicon glyphicon-edit"></i></a>
                {!! Form::button('<i class="glyphicon glyphicon-trash"></i>', ['type' => 'submit', 'class' => 'btn btn-danger btn-xs', 'onclick' => "return confirm('Are you sure?')"]) !!}
            </div>
            {!! Form::close() !!}
        </td>
    </tr>
@endforeach

My routes\web.php is like this :

Route::get('users/destroy/{year}', 'UserController@destroy')->name('users.destroy.year');

Route::resource('users', 'UserController');

My controller is like this :

public function destroy($id, $year)
{
    $user = $this->userRepository->findWithoutFail($id);

    if (empty($user)) {
        Flash::error('User not found');

        return redirect(route('users.index'));
    }

    $this->userRepository->delete($id);

    Flash::success('User deleted successfully.');

    // return redirect(route('users.index'));
    return redirect(route('users.index.year', ['year' => $year]));
}

There is exist error like this :

ErrorException in UserController.php line 205: Missing argument 2 for App\Http\Controllers\UserController::destroy()

And the url looks like this : http://localhost/mysystem/public/users/10

When click button delete, I want the url looks like this : http://localhost/mysystem/public/users/index/2020

Is there any people who can help me?

3

There are 3 answers

1
Simpledev On

Try this

$year = 2020;
{!! Form::open(['route' => ['users.destroy', $user->id, $year], 'method' => 'delete']) !!}
4
Amit Gupta On

You are using the wrong route name in your open method, it should be users.destroy.year.

So your form will look as:

{!! Form::open(['route' => ['users.destroy.year', $user->id, $year], 'method' => 'delete']) !!}
0
Simpledev On

Maybe you can try : In routes/web.php just declare the route resource

Route::resource('users', 'UserController');

In you form add an input hidden with the year value

{!! Form::open(['route' => ['users.destroy', $user->id], 'method' => 'delete']) !!}


<input type="hidden" name="year" value="2020">

And update your controller like that

public function destroy(Request $request, $id)
{
$user = $this->userRepository->findWithoutFail($id);
$year = $request->year;
if (empty($user)) {
    Flash::error('User not found');

    return redirect(route('users.index'));
}

$this->userRepository->delete($id);

Flash::success('User deleted successfully.');

// return redirect(route('users.index'));
return redirect('users/index/'.$year);

}