CRUD Laravel 4 how to link to destroy?

前端 未结 9 1410
你的背包
你的背包 2020-12-08 07:46

I will destroy my user with a HTML link, but it doesn\'t seem to generate the correct link, what am i doing wrong?

public function destroy($id)
{
    //Slet          


        
9条回答
  •  一向
    一向 (楼主)
    2020-12-08 08:17

    For those looking to create the delete button in Laravel 5:

    {!! Form::open(['action' => ['UserController@destroy', $user->id], 'method' => 'delete']) !!}
      {!! Form::submit('Delete', ['class'=>'btn btn-danger btn-mini']) !!}
    {!! Form::close() !!}
    

    This is similar to Tayler's answer but we use the new blade escape tags ( {!! and !!} ), we use the Form facade to generate the button and we use a more elegant approach to link to the controller.

    In Laravel < 5 the Forms & HTML package was pulled in automatically. In Laravel 5 we must add this package to composer.json:

    ...
    "required": {
      ...
      "laravelcollective/html": "^5.1"
    }
    ...
    

    Now add the Service Provider and Alias in config/app.php:

    ...
    'providers' => [
      ...
      Collective\Html\HtmlServiceProvider::class,
    ],
    
    'aliases' => [
      ...
      'Form' => Collective\Html\FormFacade::class,
      'Html' => Collective\Html\HtmlFacade::class,
    ],
    

    The above will output

    If you are using a different form builder just make sure it generates something similar to the above.

提交回复
热议问题