Edit File: CompanyResource.php
<?php namespace App\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource; use App\Models\Category; use App\Http\Resources\CitiesResource; use App\Http\Resources\ReviewsResource; use App\Models\Country; use URL; class CompanyResource extends JsonResource { /** * Transform the resource into an array. * * @param \Illuminate\Http\Request $request * @return array */ public static function directDistance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo) { $earthRadius = 6371000; // convert from degrees to radians $latFrom = deg2rad($latitudeFrom); $lonFrom = deg2rad($longitudeFrom); $latTo = deg2rad($latitudeTo); $lonTo = deg2rad($longitudeTo); $lonDelta = $lonTo - $lonFrom; $a = pow(cos($latTo) * sin($lonDelta), 2) + pow(cos($latFrom) * sin($latTo) - sin($latFrom) * cos($latTo) * cos($lonDelta), 2); $b = sin($latFrom) * sin($latTo) + cos($latFrom) * cos($latTo) * cos($lonDelta); $angle = atan2(sqrt($a), $b); $in_km = ($angle * $earthRadius) / 1000; $in_km += (40 / 100) * $in_km; $in_km = number_format((float)$in_km, 2, '.', ''); return $in_km; } public function toArray($request) { $distance=0; if(isset($request['receive_lat']) && isset($request['receive_long'])){ $distance = static::directDistance($request['receive_lat'], $request['receive_long'], $this->lat, $this->long); } $company = $this->delegateCompany()->latest()->first(); $bank_info = [ 'bank_account_number' => $company->bank_account_number ?? '', 'bank_account_owner' => $company->bank_account_owner ?? '', 'bank_name' => $company->bank_name ?? '', 'bank_iban_number' => $company->bank_iban_number ?? '', ]; $country = Country::where("calling_code", $this->country_key)->first(); $flag = $country ? ($country->flag ? $country->flagPath : '') : ''; $reviews = $this->ratings()->orderBy('created_at', 'desc')->get()->take(3); return [ 'id' => $this->id, 'name' => $this->name ?? '', 'completed_info' => $this->completed_info == 'true' ? true : false, 'country_key' => $this->country_key ?? '', 'country_image' => $flag, 'phone' => $this->fullPhone, 'changed_phone' => $this->changed_phone ? $this->fullChangedPhone : '', 'avatar' => $this->avatarPath ?? '', 'lat' => $this->lat ?? '', 'long' => $this->long ?? '', 'address' => $this->address ?? '', 'distance'=>$distance, 'rate' => $this->rate ?? '0.0', 'is_available'=>$this->is_available, 'approve' => $this->approve ?? 'pending', 'city' => $this->city ? new CitiesResource($this->city) : null, 'category' => $company->category ? new CategoriesResource($company->category) : null, 'available' => ( string )$this->available == 'true' ? true : false, 'car_front_image' => $company->carFrontImagePath ?? '', 'driving_license_image' => $company->drivingLicenseImagePath ?? '', 'commercial_number' => $company->commercial_number ?? '', 'commercial_image' => $company->CommercialImagePath ?? '', 'car_type' => $company->carType ? new CarTypeResource($company->carType) : null, 'car_numbers' => $company->car_numbers ?? '', 'wallet' => ( string )$this->wallet ?? '0', 'num_orders' => ( string )$this->num_orders ?? '0', 'num_comments' => ( string )$this->num_comments ?? '0', 'acc_type' => $this->type ?? 'user', 'new_orders_notify' => $this->new_orders_notify == 'true' ? true : false, 'time_zone' => date_default_timezone_get(), 'date' => date_format(date_create($this->created_at), 'Y-m-d'), 'bank_info' => $bank_info, 'reviews' => ReviewsResource::collection($reviews), ]; } }
Back to File Manager