I\'m using a pivot table on the project I\'m working with to get works of users.
E.g: User::find(1)->works
gives me the works of user with ID of 1.
Whenever you call withPivot('foo')
, Laravel you do:
SELECT ... `table`.`foo` AS `pivot_foo` FROM `table` ...
MySQL in particular allows the usage of column aliases on HAVING
, GROUP BY
and ORDER BY
clauses, but not on WHERE
clauses.
Both HAVING
and WHERE
are used for filtering queries, but they behave slightly different: HAVING
is applied after GROUP BY
and WHERE
is before.
As a general SQL rule, you shouldn't use column aliases (pivot_foo
in that case) for grouping, filtering or anything like that, since it may not work with other SQL databases.
Although not recommended, it's possible to use:
return User::find(1)->works()->having('pivot_active','=','1')->get();