Home > Enterprise >  Get users that are only subscribed laravel cashier - all users not a user
Get users that are only subscribed laravel cashier - all users not a user

Time:10-08

Here is the query I tried to write and gives an error

$users =User::has('subscriptions', function (Builder $q) {
 $q->whereNotNull('ends_at');
})->get();

Getting this Error

SQLSTATE[42601]: Syntax error: 7 ERROR: SELECT * with no tables specified is not valid LINE 1: ...sers"."id" = "subscriptions"."user_id") = (select * where "e... ^ (SQL: select * from "users" where (select count(*) from "subscriptions" where "users"."id" = "subscriptions"."user_id") = (select * where "ends_at" > now) and "users"."deleted_at" is null)

when I write this code i get results but need to filter result to get a list of subscribed users without calling User::all() then loop to filter.

User::has('subscriptions')->get();

CodePudding user response:

use

$users = User::with(['subscriptions' => static function ($query) {
    $query->whereNotNull('ends_at');
}])->get();

Read Constraining Eager Loads

To query, you need to load the subscriptions relationship first.

CodePudding user response:

Solution is here

  • Related