Edit File: ApiTrait.php
<?php namespace App\Traits; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Pagination\Paginator; use Illuminate\Support\Collection; trait ApiTrait { public function paginateNum(){ return 10; } public function paginationModel($col) { $data = [ 'total' => $col->total() ?? 0, 'count' => $col->count() ?? 0, 'per_page' => $col->perPage() ?? 0, 'next_page_url' => $col->nextPageUrl() ?? '', 'perv_page_url' => $col->previousPageUrl() ?? '', 'current_page' => $col->currentPage() ?? 0, 'total_pages' => $col->lastPage() ?? 0, ]; return $data; } public function paginateModel($items, $perPage = 10, $page = null, $options = []) { $page = $page ?: (Paginator::resolveCurrentPage() ?: 1); $items = $items instanceof Collection ? $items : Collection::make($items); return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options); } public function failReturn($msg = '', $code = 401, $data = []): \Illuminate\Http\JsonResponse { # unread notifications count if request ask if (request()->has('count_notifications')) { $count = 0; if (auth()->check()) { $count = auth()->user()->unreadNotifications->count(); } $data['count_notifications'] = $count; } return response()->json([ 'user_status' => auth('api')->user() ? auth('api')->user()->status : '', 'key' => 'fail', 'msg' => $msg, 'code' => $code, 'data' => $data ]); } public function failMsg($msg = '', $code = 401): \Illuminate\Http\JsonResponse { return response()->json([ 'user_status' => auth('api')->user() ? auth('api')->user()->status : '', 'key' => 'fail', 'msg' => $msg, 'code' => $code, ]); } public function refusedMsg($msg = '', $code = 401): \Illuminate\Http\JsonResponse { return response()->json([ 'user_status' => '', 'key' => 'refused', 'msg' => $msg, 'code' => $code, ]); } public function unauthenticatedMsg($msg = '', $code = 401): \Illuminate\Http\JsonResponse { return response()->json([ 'user_status' => '', 'key' => 'unauthenticated', 'msg' => $msg, 'code' => $code, ]); } public function successReturn($msg = '', $data = [], $code = 200): \Illuminate\Http\JsonResponse { $status = auth('api')->user() ? auth('api')->user()->status : ''; if ($data && isset($data['user'])){ $user = $data['user']; $status = $user->status; } return response()->json([ 'user_status' => $status, 'msg' => $msg, 'key' => 'success', 'code' => $code, 'data' => $data ]); } public function activationReturn($msg = '', $data = [], $code = 401): \Illuminate\Http\JsonResponse { return response()->json([ 'user_status' => auth('api')->user() ? auth('api')->user()->status : '', 'msg' => __('auth.need_activation'), 'key' => 'need_activation', 'code' => $code, ]); } public function blockReturn($msg = '', $data = [], $code = 401): \Illuminate\Http\JsonResponse { return response()->json([ 'user_status' => auth('api')->user() ? auth('api')->user()->status : '', 'msg' => $msg, 'key' => 'block', 'code' => $code, ]); } public function successMsg($msg = '', $code = 200): \Illuminate\Http\JsonResponse { return response()->json([ 'user_status' => auth('api')->user() ? auth('api')->user()->status : '', 'msg' => $msg, 'key' => 'success', 'code' => $code, ]); } public function dataReturn($data, $code = 200, $msg = ''): \Illuminate\Http\JsonResponse { $status = auth('api')->user() ? auth('api')->user()->status : ''; if ($data && isset($data['user'])){ $user = $data['user']; $status = $user->status; } return response()->json([ 'user_status' => $status, 'msg' => $msg, 'key' => 'success', 'code' => $code, 'data' => $data, ]); } public function returnImage($domain, $path): string { return asset('assets/uploads/' . $domain . '/' . $path); } public function requestFailsReturn($validator, $type = 'all_in_string'): \Illuminate\Http\JsonResponse { switch ($type) { case 'all_in_string': $errors = $validator->errors()->all(); $msg = implode(',', $validator->errors()->all()); break; case 'first': $msg = $validator->errors()->first(); break; case 'all_in_array': $msg = $validator->errors()->all(); break; default: $msg = 'حدث خطأ ما'; } return $this->failMsg($msg, 401); } public function apiPaginate($collection): array { return [ 'links' => [ 'current' => $collection->currentPage() ?? 0, 'previous' => $collection->previousPageUrl() ?? 0, 'next' => $collection->nextPageUrl() ?? 0, ], 'meta' => [ 'size' => $collection->perPage() ] ]; } public function dashboardSuccessReturn($msg = '',$url=''): \Illuminate\Http\JsonResponse { return response()->json([ 'msg' => $msg, 'url' => $url, 'key' => 'success', ]); } public function dashboardFailReturn($msg = '',$url=''): \Illuminate\Http\JsonResponse { return response()->json([ 'msg' => $msg, 'url' => $url, 'key' => 'fail', ]); } }
Back to File Manager