Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query withTrash, soft delete laravel

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?

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'));
    }
}
like image 407
EunJi Avatar asked Oct 28 '25 15:10

EunJi


1 Answers

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

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

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

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

Also, onlyTrashed() will do what it suggests:

$trashedusers = User::onlyTrashed()->get(); //Only soft deleted users
like image 123
samiles Avatar answered Oct 30 '25 14:10

samiles



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!