Search code examples
laravelsoft-delete

Soft delete still being shown in view laravel


for some reason when I try to do a soft delete on my data, they didn't disappear in my view(home.blade.php) but inside my database, the deleted_at column has been recorded showing the date when I delete it. Could you help me check my code is there any problem with it, thank you

Here are my codes:

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:

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

Personal_info model:

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');
    }

Solution

  • Eloquent queries will, when querying a model that uses soft deletes, automatically exclude the soft deleted models from all query results.

    However, you are not using an Eloquent query, rather a raw DB query. Therefore you will need to also check where the deleted_at column is not null in your query.