【问题标题】:delete a row from the database using an id button laravel 5.6?使用 id 按钮 laravel 5.6 从数据库中删除一行?
【发布时间】:2018-10-18 12:05:51
【问题描述】:

我有一个包含 2 个组件 id 和 body 的数据库表,body 是一个文本,正如我在 laravel 数据库迁移中指定的那样:

$table->increments('id');
$table->text('body');

我用我的终端 php artisan make:controller todoController --resource 创建了一个文件控制器 todocontroller --resource 并使用以下命令,我有很多功能:它们之间有 3 个与 id 有关系:

public function show($id)
    {
      return $id ;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
      DB::table('todos')->where('id', '=', $id)->delete();
   return view('pages.home');;
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

我想从我在 Home.blade.php 中创建的简单单击按钮中删除我喜欢的行:

<ul class="list-group">
    @foreach($todos as $todo)
  <li class="list-group-item d-flex justify-content-between align-items-center">
  <form action="" method="">
  <button type="button" class="btn btn-success" >{{$todo->id}}</button>
</form>
  </li>
    @endforeach
</ul>

我多次尝试使用php artisan route:list 从终端获取root,但它不起作用。

【问题讨论】:

标签: php html laravel


【解决方案1】:

为了删除资源路由,你可以写一个新的路由

Route::get('/todo/delete/{id},'todoController@destroy')->name('todo.destroy');


<ul class="list-group">
@foreach($todos as $todo)
  <li class="list-group-item d-flex justify-content-between align- 
 items-center">
  <form action="" method="">


  <a href="{{route('todo.destroy',$todo->id)}}"><button type="button" class="btn btn-success" >Delete</button></a
 </form>
  </li>
@endforeach

public function destroy($id)
{

}

【讨论】:

    【解决方案2】:

    在您的控制器中:

    public function destroy($id)
    {
        $todo= Todo::find($id);
        $todo->delete();
        return redirect()->route('homepage');
    }
    

    在视图中:

    @foreach(...)
        {!! Form::open(['route' => ['todos.destroy', $todo->id], 'method' => 'DELETE']) !!}
        {!! Form::submit('DELETE') !!}
        {!! Form::close() !!}
    @endforeach
    

    【讨论】:

      【解决方案3】:

      在 laravel 5.6 中这很简单,只需使用下面的代码根据行的 id 删除数据库的一行:

      1- 首先在 web.php 中为控制器的删除方法定义名称,我们需要它以刀片形式调用

      Route::delete('todos/{todo}', 'TodoController@destroy')-&gt;name('todos.delete');

      2- 在刀片文件中只需创建如下所示的表单(只需将其替换为您在 foreach 循环中的表单)

      <form method="post" action={{ route('todos.delete') , ['id' => $todo->id] }}> {!! csrf_field() !!} <input type="hidden" value="DELETE"/> <button type="submit">click to delete this row base on id</button>

      3- 最后在你的控制器的销毁方法中

      $todo = Todo::whereId($id); $todo->delete(); return back();

      希望能为你工作的朋友:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-13
        • 2013-01-03
        • 2017-06-02
        • 2021-04-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多