Laravel Raw查询订单

时间:2018-05-29 18:36:11

标签: mysql sql laravel

我有多态关系。

Class Reply

public function replyable()
{
    return $this->morphTo();
}

属于UserTrainer

当我获取回复时,我希望首先放置经过身份验证的用户回复。

因此,如果认证用户是培训师,培训师的回复应该是第一个,而认证用户是普通用户,则用户的回复应该是第一个。

这就是我现在所得到的。

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}}

0 个答案:

没有答案