问题描述
我正在尝试从表消息中获取被破坏的行:
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();
}
我收到此错误:
Method Illuminate\Database\Query\Builder::onlyTrashed does not exist.
我检查了Builder和SoftDeletes文件中的onlyTrashed方法,但它不存在,如何从消息表中查找被垃圾消息?
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?
我想到的唯一方法是创建一种方法,该方法不返回delete_at不为null的消息,并且使回收站仅返回不为null的消息.但是我仍然想知道为什么这不起作用,因为它在此URL的文档中:
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
更多信息
是的,它在模型内部,是的,我添加了使用SoftDeletes的方法:
Yes it is inside model and yes I added use SoftDeletes:
use Illuminate\Database\Eloquent\SoftDeletes;
-位于顶部
use SoftDeletes;
打开课程后
让我在此处粘贴整个模型:
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();
}
}
并且控制器具有:
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'));
}
我也尝试过调用static,并且尝试通过 tinker 进行调用,并且仍然不断获得:
I tried calling static as well and I tried doing it from tinker and still keep getting:
onlyTrashed()不存在
onlyTrashed() doesn not exist
推荐答案
您必须调用父构造函数:
You have to call the parent constructor:
public function __construct() {
parent::__construct();
$this->_u = auth()->user()->user_id;
}
这篇关于在查询生成器中不存在Only OnlyTrashed()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!