1

I have a Categories table.

It has a foreign key of question_category in the questions table

I am looping through 4 of my Categories like so :

$categories = MockCategories::take(4)->get();

In the MockCategories Model, I'd like to add a "num_questions" property. Which in turn will run a query in the questions table and return a count of number questions based on that category.

Can I do this? Is there maybe a better way of doing it? I'm thinking maybe adding a relationship of hasMany and linking to the questions, Then showing a count of them.

Any help appreciated.

Thanks

1 Answer 1

1

You can do something like this in the MockCategories model:

private $num_questions = 4;

public function scopeTakeFour($query) {
   $query->take($this->num_questions )->get();
}

and in your controller you simply do:

MockCategories::takeFour();
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.