软删除在桌子上

时间:2017-10-27 08:58:15

标签: laravel soft-delete

我刚开始使用软删除,我不知道该怎么做,我只是关注某个例子,仍然不确定如何进行适当的软删除。我想要的只是删除personal_info表,但我继续收到一条没有错误信息,这让我迷失了,因为我不知道我做错了什么,有人能帮帮我吗?非常感谢

home.blade.php

    <table class="table table-bordered">
      <tr>
        <th><strong><big>Name: </big></strong></th>
        <th><strong><big>Action </big></strong></th>
      </tr>
      <td>
      <tr>
        @foreach($data as $value)
      <tr>    
      <th><a href="{{route('user.show',['id'=>$value->id])}}">{{$value->Name}}</a></th>
      <th><form action="{{  url('/home/'.$value->id.'/delete')  }}" method="post">
        <button>Delete</button>
      </form></th>               
      </tr>
        @endforeach
      </tr>
      </tr>
    </table>

控制器:

public function delete($id = 0){   
    if ($id > 0){
        personal_info::destroy($id);
    }
    return redirect("/home");
}

或者我应该这样做?

public function delete($id){
$data = personal_info::find($id)->delete();
    return redirect("/home");
}

personal_info模特:

use Illuminate\Database\Eloquent\Model;

use Eloquent;
use SoftDeletes;
class personal_info extends Eloquent
{
    protected $fillable = array('Name');
    protected $table = 'personal_infos';
    protected $primaryKey = 'id';
    protected $dates = ['deleted_at'];
        public function user_info1s() {
        return $this->hasMany('App\user_info1','user_id');
    }

路线:(不确定我应该使用DELETE)

Route::get('/home/{id}/delete', 'HomeController@delete');

enter image description here

2 个答案:

答案 0 :(得分:1)

无需对方法使用delete。尝试将您的功能更改为此

public function delete($id){   
    personal_info::findOrFail($id)->delete();
    return back();
}
  

修改

Route方法和Form方法必须相同。

答案 1 :(得分:1)

更改

Route::get('/home/{id}/delete', 'HomeController@delete');

Route::post('/home/{id}/delete', 'HomeController@delete');

控制器方法

use Carbon\Carbon;

public function delete($id)
{
  personal_info::find($id)->update([
      'deleted_at' => Carbon::now()
  ]);

  return redirect()->back();
}

home.blade.php

<table class="table table-bordered">
      <tr>
        <th><strong><big>Name: </big></strong></th>
        <th><strong><big>Action </big></strong></th>
      </tr>
      <td>
      <tr>

        // Example of adjusting your query to support soft deletes
        @php
          $data = Some\Model::whereNotNull('deleted_at')->get();
        @endphp

        @foreach($data as $value)
      <tr>    
      <th><a href="{{route('user.show',['id'=>$value->id])}}">{{$value->Name}}</a></th>
      <th><form action="{{  url('/home/'.$value->id.'/delete')  }}" method="post">
        <button>Delete</button>
      </form></th>               
      </tr>
        @endforeach
      </tr>
      </tr>
    </table>