dimanche 12 février 2017

Create an subquery alias to use it in Laravel Query Builder

I want to create something like an alias to SQL subquery so I can use it like a plain SQL field. For example, I've got 3 tables: user, user_comments and user_comment_answers. If I want to get count of all answers to comments of current user, I'll do something like this:

SELECT
  COUNT(*) as comment_answers_count
  FROM user_comment_answers
  WHERE id IN (SELECT id FROM user_comments WHERE user_id = :id)

Now I want to make Laravel treat this as a plain field of user table, so I can simply do stuff like this:

echo Auth::user()->comment_answer_count

or like this

$popularUsers = User::where('comment_answer_count', '>=', 100)

How can I achieve this in Laravel 5.4?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2kYtONq
via IFTTT

Aucun commentaire:

Enregistrer un commentaire