查询带有垃圾桶的软删除Laravel

4
基本上,我成功地让我的用户表使用了软删除。问题是现在我的其他页面无法工作,我认为我需要在查询页面时使用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'));
    }
}

请查看此链接:https://stackoverflow.com/a/45669204/1838205 - itzmebibin
1个回答

10

你只需在查询中添加->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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接