Edit File: BaseTrait.php
<?php namespace App\Traits; trait BaseTrait { /** * Paginate */ public function paginate($request) { $this->model->search($request->searchArray)->paginate($request->per_page); } /** * Get All */ public function getAll() { return $this->model::get(); } /** * Find by id */ public function findById($id) { return $this->model::findOrFail($id); } /** * Get the model validation request path. */ public function getRequestPath() { // Get the function name/ request file name $functionName = ucfirst(request()->route()->getActionMethod()); // Get the request file path $path = "App\Http\Requests\\" . ucfirst(request()->route()->getPrefix()) . "\\" . $this->model->getFolderName() . "\\" . $functionName; // Check if the request file exists or not and return the path return class_exists($path) ? $path : "Illuminate\Http\Request"; } /** * Get the model collection. */ public function apiCollectionResponse($data) { $collection = "App\Http\Resources\Api\\" . ucfirst($this->model->getFolderName()) . "\\" . ucfirst($this->model->pluralModelName()) . "Collection"; // check if the collection exists or not // and return the collection return class_exists($collection) ? $collection::make($data) : $data; } /** * Get the model resource. */ public function apiResourceResponse($data) { $resource = "App\Http\Resources\Api\\" . ucfirst($this->model->getFolderName()) . "\\" . ucfirst($this->model->pluralModelName()) . "Resource"; // check if the resource exists or not // and return the resource return class_exists($resource) ? $resource::make($data) : $data; } /** * Check if the request is from the API or not. */ public function isApiRequest() { return request()->is('api/*'); } }
Back to File Manager