我有多态关系。
Class Reply
public function replyable()
{
return $this->morphTo();
}
属于User
和Trainer
。
当我获取回复时,我希望首先放置经过身份验证的用户回复。
因此,如果认证用户是培训师,培训师的回复应该是第一个,而认证用户是普通用户,则用户的回复应该是第一个。
这就是我现在所得到的。
public function index(Question $question)
{
if (!! trainer()) {
//if an auth user is a trainer, place their posts first in the response data
return $question->replies()->orderByRaw("FIELD(replyable_id, ".trainer()->id.") DESC")->inRandomOrder()->with('replyable', 'question')->paginate(15);
}
if (auth()->check()) {
//if an auth user is a normal user, place their posts first in the response data
return $question->replies()->orderByRaw("FIELD(replyable_id, ".auth()->id().") DESC")->inRandomOrder()->with('replyable', 'question')->paginate(15);
}
return $question->replies()->with('replyable', 'question')->inRandomOrder()->paginate(15);
}
我需要根据replyable_type
限制查询。
因此,auth用户是培训师,应首先订阅回复,其中replyable_type
App\Trainer
replyable_id
的回复只有 if (!! trainer()) {
//if an auth user is a trainer, place their posts first in the response data
return $question->replies()->orderByRaw("where replyable_type = trainers FIELD(replyable_id, ".trainer()->id.") DESC")->inRandomOrder()->with('replyable', 'question')->paginate(15);
}
经过身份验证的用户(培训师)ID。
此查询无效。
{{1}}