【发布时间】:2017-11-12 14:19:13
【问题描述】:
如何将withTrashed 应用于hasManyThrough 关系?
$this->hasManyThrough('App\Message', 'App\Deal')->withTrashed();
返回
调用未定义的方法 Illuminate\Database\Query\Builder::withTrashed()
当我在做的时候:
$messages = Auth::user()->messages()->with('deal')->orderBy('created_at', 'DESC')->get();`
这是我的交易模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Deal extends Model
{
use SoftDeletes;
/* ... */
protected $dates = ['deleted_at'];
public function user() {
return $this->belongsTo('App\User');
}
public function messages() {
return $this->hasMany('App\Message'); // I've tried to put withTrashed() here, there is no error but it doesn't include soft deleting items.
}
}
【问题讨论】:
-
你是否也在
Message模型中使用SoftDeletestrait? -
不,邮件永远不会被删除
-
在
->hasManyThrough('App\Message', 'App\Deal')->withTrashed()这一行中,您请求带有已删除消息的消息,并且您没有使用SoftDeletetrait,这就是引发异常的原因,您必须删除withTrashed函数 -
我怎样才能要求属于已删除交易的消息?
-
我发现这种方式没有办法,我添加一个答案也许对你有帮助
标签: laravel laravel-5 eloquent