| 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 App\Helpers\UtilityHelper;
use App\Traits\UploadTrait;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class MachineType extends Model
{
use HasFactory, UploadTrait;
protected $fillable = ['name', 'icon'];
protected $hidden = [
'updated_at',
'created_at'
];
public function advertisements()
{
return $this->hasMany(Advertisement::class, 'id', 'ad_id');
}
public function models()
{
return $this->hasMany(MachineModel::class, 'machine_type_id', 'id');
}
public function getIconAttribute($value)
{
$folder = config("constants.MACHINE_TYPE");
return $this->getURL($folder, $value);
}
public function scopeSearchKeyword(Builder $builder, $keyword): void
{
$builder->when($keyword, function ($query) use ($keyword) {
$query->Where('name', 'LIKE', '%' . $keyword . '%');
});
}
public const PerPageRecord = "10";
public static function GetMachineTypes($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::query()
->searchKeyword($keyword)
->orderBy($sort_by, $order_by);
$countQuery = (clone $query);
$total = $countQuery->count();
$perPage = self::PerPageRecord;
$currentPage = max(1, (int)$offset + 1); // since your offset is page-indexed (0-based)
$totalPages = $perPage > 0 ? (int) ceil($total / $perPage) : 0;
$query->orderBy($sort_by, $order_by)
->offset($offset * $perPage)
->limit($perPage);
$list = $query->get();
$nextOffset = ($list->count() === $perPage && $currentPage < $totalPages) ? ($offset + 1) : -1;
$data = [
'count' => $total,
'list' => $list,
'current_page' => $currentPage,
'next_offset' => $nextOffset,
'per_page' => $perPage,
'total_pages' => $totalPages,
];
return $data;
}
}