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.
}
}
答案 0 :(得分:4)
对于所有来迟的人,现在有一种使用Laravel进行此操作的本地方法。
$this->hasManyThrough('App\Message', 'App\Deal')->withTrashedParents();
此文档记录不充分,但可以在Illuminate\Database\Eloquent\Relations\HasManyThrough
答案 1 :(得分:1)
引发错误是因为您在SoftDelete
模型中未使用Message
特征而请求已删除邮件的邮件。
在检查hasManyThrough
关系代码后,我发现没有办法以这种方式进行,你应该玩。
例如:
使用消息获取用户的交易
$deals = Auth::user()->deals()->withTrashed()->with('messages')->get();
foreach($deals as $deal) {
//Do your logic here and you can access messages of deal with $deal->messages
}