如何从 Laravel 中的 table 获取所有行(没有软删除)?

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

我用laravel 5.3

我用这个:https://github.com/jenssegers/laravel-mongodb

我的laraveleloquent是这样的:

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

我的模型是这样的:

<?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'];
    ...
}

问题是,执行时,软删除项出现在那里。怎么了?

您可以覆盖 Employee Class 中的 toArray() 方法。

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

您使用了错误的特征 use Illuminate\Database\Eloquent\SoftDeletes; 仅适用于 Laravel Eloquent 模型。

对于 Jenssegers\Mongodb 你需要使用:

use Jenssegers\Mongodb\Eloquent\SoftDeletes;