软删除仍然在视图中显示

时间:2017-10-30 00:42:48

标签: laravel soft-delete

出于某种原因,当我尝试对我的数据进行软删除时,它们并没有在我的视图(home.blade.php)中消失,但在我的数据库中,已记录的deleted_at列显示了日期时我删除它。你能帮我检查一下我的代码是否有任何问题,谢谢

以下是我的代码:

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="get">
        {{ csrf_field() }}
        <button type="submit">Delete</button>
      </form></th>               
      </tr>
        @endforeach
      </tr>
      </tr>
    </table>

HomeController中:

    public function getData(){
        $data['data'] = DB::table('personal_infos')->get()->sortByDesc('created_at');

        if(count($data)>0){
        return view('home',$data);
    }else{
    return view('home');
}

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

路线:

Route::get('/home/{id}/delete', 'HomeController@delete');
Route::get('user/show/{id}','HomeController@getInfo')->name("user.show");

Personal_info模型:

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Eloquent;

class personal_info extends Eloquent
{
    use SoftDeletes;
    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');
    }

1 个答案:

答案 0 :(得分:3)

Eloquent 查询在查询使用软删除的模型时,会自动从所有查询结果中排除软删除的模型。

但是,您没有使用Eloquent查询,而是使用原始数据库查询。因此,您还需要检查查询中deleted_at列不为空的位置。