6

I am trying to get trashed rows from table messages:

public function trash() {
    return $this->onlyTrashed()
        ->where('user_id', '=', $this->_u)
        ->orWhere('receiver', '=', $this->_u)
        ->orderBy('deleted_at', 'desc')->get();
}

I get this error:

Method Illuminate\Database\Query\Builder::onlyTrashed does not exist.

I checked up Builder and SoftDeletes files for onlyTrashed method and it does not exist, how can I look up to trashed messages from message table?

The only way I think about is to create method that doesn't return messages where delete_at is not null and for trashed to return only those where it is not null. But I am still wondering why this doesn't work since it is in documentation at this url:

https://laravel.com/docs/5.6/eloquent#soft-deleting

MORE INFO

Yes it is inside model and yes I added use SoftDeletes:

use Illuminate\Database\Eloquent\SoftDeletes; - on top

use SoftDeletes; after opening the class

Let me paste entire model here:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class Messages extends Model
{

use SoftDeletes;

protected $fillable = [
    'user_id', 'subject', 'text', 'receiver'
];

public $_u;

protected $dates = ['deleted_at'];

public function __construct() {
    $this->_u = auth()->user()->user_id; //or some id as string
}

public function trash() {
        return $this->onlyTrashed()
        ->where('user_id', '=', $this->_u)
        ->orWhere('receiver', '=', $this->_u)
        ->orderBy('deleted_at', 'desc')->get();
}

public static function trashed() {
        return self::onlyTrashed();
}
}

And controller has:

public function __construct() {
        $this->middleware('auth');
    }

public function index($field = 'trash') {
    if ($field !== "new") {
        $messages = (new Msg)->$field();
        $user = auth()->user();
        return view('pages.messages', compact('messages', 'user'));
    }
    return view('pages.messages.new', compact('messages', 'user'));
}

I tried calling static as well and I tried doing it from tinker and still keep getting:

onlyTrashed() doesn not exist

Jonas Staudenmeir
  • 24,815
  • 6
  • 63
  • 109

4 Answers4

4

You have to call the parent constructor:

public function __construct() {
    parent::__construct();

    $this->_u = auth()->user()->user_id;
}
Jonas Staudenmeir
  • 24,815
  • 6
  • 63
  • 109
2

I think what you want is to define the trash method static:

public static function trash() {
    return self::onlyTrashed()
        ->where('user_id', '=', $this->_u)
        ->orWhere('receiver', '=', $this->_u)
        ->orderBy('deleted_at', 'desc')->get();
}

Then call this function by:

$messages = Messages::trash();
aceraven777
  • 4,358
  • 3
  • 31
  • 55
1

This should work

# YourModelController.php
    /**
     * Show only trashed 
     *
     * @return \Illuminate\Http\Response
     */
    public function trashed()
    {
...
        $trashed = YourModel::onlyTrashed()->get();
...
    }
Hahdin
  • 41
  • 2
0

I have researched a little further and I got this:

From https://laravel.com/api/5.6/Illuminate/Database/Eloquent.html

I should have

SoftDeletesTrait

but I have

SoftDeletes

. In softdeletestrait we have onlyTrashed method but in SoftDeletes we do not.

So I copied that method from this page: https://github.com/laravel/framework/blob/7d9e7068c49f945385673014d4cba4de28accd5e/src/Illuminate/Database/Eloquent/SoftDeletingTrait.php#L119

And added it to SoftDeletes class, now it works like it should. I haven't find why it doesn't exist inside SoftDeletes class so if anyone find out let us know!