简体   繁体   English

Laravel中如何使用查询结果和LIKE语句再次查询

[英]How to use query result and query again using LIKE statement in Laravel

I am trying to use questions result and get question again if title has some char in it.如果标题中有一些字符,我正在尝试使用问题结果并再次提问。 If I query in condition_question table, I get results as expected.如果我在 condition_question 表中查询,我会得到预期的结果。

public function showQuestions($category)
    {
        $myArray = array($category);

        $questions = Question::whereIn('question_id', function ($query) use ($myArray) {
            $query->select('question_id')
                ->from('condition_question')
                ->whereIn('condition_id', $myArray);
        })->orderBy('question_id', 'desc')->paginate(20);

        return QuestionLiteResource::collection($questions);
}

Question: How can I use now $questions result and query again with LIKE statement.问题:我现在如何使用 $questions 结果并使用 LIKE 语句再次查询。 So far I tried many thing, for example like this, but something is missing as I am getting errors:到目前为止,我尝试了很多东西,例如像这样,但是由于出现错误而丢失了一些东西:

public function showQuestions($category, $queryQuestion)
    {
        $myArray = array($category);

        $chary = $queryQuestion;

        $questions = Question::whereIn('question_id', function ($query) use ($myArray) {
            $query->select('question_id')
                ->from('condition_question')
                ->whereIn('condition_id', $myArray);
        })->get();

        $results = $questions->where('question_title', 'LIKE', "%{$chary}%")->get();

        return QuestionLiteResource::collection($results->values());
}

I know it is not my best, but need some help...It would be also cool to have paginated result at the end.我知道这不是我最好的,但需要一些帮助......最后有分页结果也很酷。 So, how to get collection of questions from questions table where title has char.那么,如何从标题为字符的问题表中获取问题的集合。 Any help would be most welcomed!任何帮助将是最受欢迎的!

You might know that once you call get() function, you got the results and not able to query any further.您可能知道,一旦您调用 get() 函数,您将获得结果并且无法进一步查询。 Maybe this is gonna work:也许这会奏效:

public function showQuestions($category, $queryQuestion)
    {
        $myArray = array($category);

        $chary = $queryQuestion;

        $questions = Question::whereIn('question_id', function ($query) use ($myArray) {
            $query->select('question_id')
                ->from('condition_question')
                ->whereIn('condition_id', $myArray);
        })
        ->where('question_title', 'LIKE', "%{$chary}%")
        ->get();

        return QuestionLiteResource::collection($questions);
}

Since you have called get() on question query, you get the result as an Laravel Collection .由于您在问题查询中调用了get() ,因此您将获得Laravel Collection的结果。

To filter through collection you can use filter() function.要过滤集合,您可以使用filter()函数。

Example Code示例代码

$results = $questions->filter(function($question) use ($chary) {
    return Str::contains($question->question_title, $chary);
});
i think you can use join():

        public function showQuestions($category, $queryQuestion)
        {
            $myArray = array($category);
            $chary = $queryQuestion;

            $query = Question::getModel()->newQuery();
            $questions = $query
                 ->join('condition_question', function (Builder $join) use ($myArray) {
                   $join->on('questions.question_id', '=', 'condition_question.question_id');
                   $join->whereIn('condition_question.condition_id', $myArray);
                 }) 
                 ->where('questions.question_title', 'like', $chary)
                 ->orderBy('questions.question_id', 'desc')
                 ->paginate(20)

            return QuestionLiteResource::collection($questions);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM