Laravel billable get 订阅未结束的记录

Laravel billable get records where subscription not ended

我正在使用 Laravel Billable,我已经将它连接到不同的模型(客户端)并且一切正常。但是,我正在尝试获取订阅结束日期为空的模型上的所有记录。我正在执行以下操作;

$collection = Model::where('client.subscriptions', function ($q) {
    $q->where('ends_at', NULL);
})->get();

我收到以下错误:

Undefined table: 7 ERROR: missing FROM-clause entry for table "client" LINE 1: select * from "table" where "client"."subscriptions" =... ^ (SQL: select * from "table" where "client"."subscriptions" = (select * where "ends_at" is null))

更新

代码更新

   $jobs = \App\Models\Job::with(['client' => function($query) {
    $query->whereHas('subscriptions', function ($query){
        $query->where('ends_at', NULL);
    });
}])->get();

示例数据库

(model) Job (db:name = 'jobs')

id | client_id | name

(model) Client (db:name = 'clients')

id | name 

(model) Subscription (db:name = 'subscriptions')

id | client_id | stripe_plan | trial_ends_at | ends_at 

客户端模型

/**
 * Get all of the subscriptions for the Stripe model.
 *
 * @return \Illuminate\Database\Eloquent\Relations\HasMany
 */
public function subscriptions()
{
   // foreign key is client_id
    return $this->hasMany(Subscription::class, $this->getForeignKey())->orderBy('created_at', 'desc');
}

工作模式

public function client()
{
    return $this->belongsTo(Client::class);
}

现在使用这个查询;

 $jobs = \App\Models\Job::with(['client' => function($query) {
        $query->whereHas('subscriptions', function ($query){
            $query->where('ends_at', NULL);
        });
    }])->get();

我仍然得到订阅 ends_at 不为空的记录。知道我做错了什么吗?

如果您使用的是 Illuminate\Database\Eloquent 命名空间中定义的基本模型 class,我认为您错了。

你应该直接使用你的模型(从你的 question/code 看来它被称为客户端)并做这样的事情:

$collection = Client::where('subscriptions', function ($q) {
    return $q->whereNull('ends_at');
})->get();

正确的查询是;

$jobs = Job::whereHas('client', function($query) {
    $query->whereHas('subscriptions', function ($query) {
        $query->where('ends_at', NULL);
    });
})->get();