| Server IP : 13.235.167.51 / Your IP : 216.73.217.116 Web Server : Apache System : Linux machinox-server 6.17.0-1013-aws #13~24.04.1-Ubuntu SMP Fri Apr 24 21:36:58 UTC 2026 aarch64 User : root ( 0) PHP Version : 8.2.29 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/app.machinox.in/app/Models/ |
Upload File : |
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Subscription extends Model
{
use HasFactory;
protected $fillable = [
'name',
'type',
'tax_percent'
];
protected $hidden = [
'created_at',
'updated_at'
];
public function transactions()
{
return $this->hasMany(Transaction::class, 'id', 'subs_id');
}
public function userSubscriptions()
{
return $this->hasMany(UserSubscription::class);
}
public function plans()
{
return $this->hasMany(SubscriptionPlan::class, 'sub_id', 'id');
}
public const PerPageRecord = "10";
public static function GetSubscriptions($request)
{
$keyword = getValue($request->input('keyword'));
$offset = getValue($request->input('offset'), 0);
$sort_by = getValue($request->input('sort_by'), 'created_at');
$order_by = getValue($request->input('order_by'), 'DESC');
$query = self::with('plans')
->when($keyword, function ($query) use ($keyword) {
$query->where(function ($q) use ($keyword) {
$q->where('name', 'LIKE', '%' . $keyword . '%')
->orWhereHas('plans', function ($q2) use ($keyword) {
$q2->where('duration', 'LIKE', '%' . $keyword . '%')
->orWhere('amount', 'LIKE', '%' . $keyword . '%')
->orWhere('discount_amount', 'LIKE', '%' . $keyword . '%')
->orWhere('description', 'LIKE', '%' . $keyword . '%');
});
});
})
->orderBy($sort_by, $order_by);
$data['count'] = $query->count();
$query->offset($offset * self::PerPageRecord)
->limit(self::PerPageRecord);
$subscriptions = $query->get();
$formatted = [];
foreach ($subscriptions as $subscription) {
$subData = [
'id'=> $subscription->id,
'name' => $subscription->name,
'tax_percent' => $subscription->tax_percent
];
foreach ($subscription->plans as $plan) {
$typeKey = $plan->type;
$subData[$typeKey][] = [
'id' => $plan->id,
'duration' => $plan->duration,
'amount' => $plan->amount,
'discount_amount' => $plan->discount_amount,
'description' => $plan->description,
];
}
$formatted['list'][] = $subData;
}
$formatted['count'] = $data['count'];
$formatted['next_offset'] = (isset($formatted['list']) && count($formatted['list']) == self::PerPageRecord)
? ($offset + 1)
: -1;
return $formatted;
}
}