laravel 查询withTrash,软删除laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33498934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:37:37  来源:igfitidea点击:

Query withTrash, soft delete laravel

laravellaravel-5.1

提问by EunJi

Basically I manage to get my soft delete working on my User table., problem is now my other pages would not work, I believe I need to make some changes using the withTrashed to the queries for the pages? For example the controller as shown below, how do I add the users column which have been soft deleted, can someone guide me and help me with this?

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

Controller.php

控制器.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'));
    }
}

回答by samiles

You can just add ->withTrashed()to your query and use getinstead of all. Like so:

您可以添加->withTrashed()到您的查询并使用get而不是all. 像这样:

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

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

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

Also, onlyTrashed()will do what it suggests:

此外,onlyTrashed()将按照它的建议进行操作:

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