查询withTrash,软删除laravel

时间:2015-11-03 12:16:57

标签: laravel laravel-5.1

基本上我设法让我的软删除工作在我的用户表上。问题是现在我的其他页面不起作用,我相信我需要使用withTrashed对页面的查询进行一些更改?例如控制器如下所示,如何添加已被软删除的用户列,有人可以指导我并帮助我吗?

Controller.php这样

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Zone;
use App\Parameter;

class DashboardController extends Controller
{
    public function index() {
        $zones = Zone::all();
        $parameters = Parameter::all();

        return view('dashboard', compact('zones', 'parameters'));
    }
}

1 个答案:

答案 0 :(得分:4)

您只需在查询中添加->withTrashed(),然后使用get代替all。像这样:

$zones = Zone::all(); //Excludes soft deleted

$allparameters = Parameter::withTrashed()->get(); //Includes soft deleted

$allusers = User::withTrashed()->get();

此外,onlyTrashed()会按照它的建议行事:

$trashedusers = User::onlyTrashed()->get(); //Only soft deleted users