简体   繁体   中英

How to get all rows (without soft deleted) from a table in Laravel?

I use laravel 5.3

I use this : https://github.com/jenssegers/laravel-mongodb

My laravel eloquent is like this :

$data = Employee::select('id', 'name', 'salary', 'description')
                ->find($id);

My model is like this :

<?php
namespace App\Models;
use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent; 
use Jenssegers\Mongodb\Eloquent\HybridRelations;
class Employee extends Eloquent
{
    use HybridRelations;  
    use SoftDeletes;
    protected $connection = 'mongodb';
    public $table = 'employees';
    protected $dates = ['deleted_at'];
    ...
}

The problem is, when executed, the soft deleted items appears there. What is wrong?

You are using the wrong trait use Illuminate\\Database\\Eloquent\\SoftDeletes; only works for the Laravel Eloquent models.

For Jenssegers\\Mongodb you need to use:

use Jenssegers\Mongodb\Eloquent\SoftDeletes;

You can override toArray() method in Employee Class.

function toArray() {
    return [
        'id'        =>    $this->id,
        'name'      =>    $this->name,
        'salary'    =>    $this->salary,
         ...etc // dont put deleted_at here
    ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM