lundi 9 décembre 2019

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.

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. 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!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire