1

I have a working function in my controller, but I want to place a part of the code in model, is repeatable and I want to use it in multiple controllers. but witch my current code is not working (no errors).

Controller

public function index(Request $request)
{
   $query = new Question;
   $query->select(
       ['questions.id', 
       'questions.free_text', 
       'questions.title', 
       'questions.created_at',
       'lkp_answers.name as bestMatch',
       ]);
   $query->with(['answer_history']);
   $query->join('lkp_answers', 'lkp_answers.id', '=', 'questions.best_match');
   $query->count_filter($query, $request); //here I try to use it
   return response()->json($query->paginate($request->per_page));
}

Model

public function count_filter($query, $request){
    if($request->sort_by){
        $direction = ($request->sort_direction === 'false') ? 'asc' : 'desc';

        if($request->sort_by === 'best_match'){
            $query->orderBy('lkp_answers.name', $direction);
        }else if ($request->sort_by === 'noneOfTheAbove') {
            $query->withCount(['answer_history AS none' => function ($q) {
                $q->where('answer_type', 'none');

                return $q;
            }])->orderBy('none', $direction);
        } else if ($request->sort_by === 'skipped') {
            $query->withCount(['answer_history AS skipped' => function ($q) {
                $q->where('answer_type', 'skipped');

                return $q;
            }])->orderBy('skipped', $direction);
        }  else if ($request->sort_by === 'totalVotes') {
            $query->withCount(['answer_history AS totalVotes' => function ($q) {
                $q->where('answer_type', '!=','skipped');

                return $q;
            }])->orderBy('totalVotes', $direction);
        } 
        else {
            $query->orderBy($request->sort_by,$direction);
        }
    }
    return $query;
}
5
  • There is another option is to make helper function and put that code there so you can use anywhere in project. Commented May 11, 2020 at 6:05
  • Ok, but that should work from Model too, but if I dd($query->get()), at the beginning of the model function, I don't have the bestMatch, and relationship from join.. Commented May 11, 2020 at 6:09
  • try $query = $query->count_filter($query, $request); Commented May 11, 2020 at 6:10
  • Same result. I don't get values from join.. Commented May 11, 2020 at 6:13
  • @Beusebiu $query = Helper::count_filter($query, $request); and make static function on helper file. and call like that. Commented May 11, 2020 at 6:18

1 Answer 1

1

The problem is that you defined the method to the model but you try to call it on an eloquent query. What you need to do is to use another variable for the query:

public function index(Request $request, Question $question)
{
   $query = $question->newQuery();
   $query->select(
       ['questions.id', 
       'questions.free_text', 
       'questions.title', 
       'questions.created_at',
       'lkp_answers.name as bestMatch',
       ]);
   $query->with(['answer_history']);
   $query->join('lkp_answers', 'lkp_answers.id', '=', 'questions.best_match');

   $question->count_filter($query, $request)

   return response()->json($query->paginate($request->per_page));
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.