Edit File: BaseService.php
<?php namespace App\Services\Entity; use App\Traits\BaseTrait; use App\Traits\ReportTrait; use Illuminate\Database\Eloquent\Model; use Illuminate\Contracts\Foundation\Application; use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\View; use Illuminate\Http\JsonResponse; class BaseService { use BaseTrait; protected $model; public function __construct(Model $model) { $this->model = $model; } /* |-------------------------------------------------------------------------- | WEB Functions |-------------------------------------------------------------------------- | */ /** * Show the index page of the model. * * @return Application|Factory|View|JsonResponse */ public function index() { // todo move return to the controller if (request()->ajax()) { // get the rows from the database ${$this->model->getFolderName()} = $this->model::search(request()->searchArray)->paginate(30); // render the table view $html = view('admin.' . $this->model->getFolderName() . '.table', get_defined_vars())->render(); // return the rendered view as a json response return response()->json(['html' => $html]); } // return the index view return view('admin.' . $this->model->getFolderName() . '.index'); } /** * Show the create page of the model. * * @return Application|Factory|View */ public function create() { // return the create view return view('admin.' . $this->model->getFolderName() . '.create'); } /** * Store a newly created model in storage. * * @param \Illuminate\Http\Request $request * @return array */ public function store($request) { // Check if the request is validated or not // and get the data $data = method_exists($request, 'validated') ? $request->validated() : $request->all(); // create a new instance of the model // and save it to the database $model = $this->model::create($data); // add a log entry // using the ReportTrait // the log entry will be in the format // "add_{lower_model_name}" // for example, if the model is named "User" // the log entry will be "add_user" ReportTrait::addToLog(__('admin.' . $this->model->getFolderName() . '.store')); // check if the request is api // and return a json response if ($this->isApiRequest()) { return $model; } // with a success status return ['status' => 'success', 'url' => route('admin.' . $this->model->getFolderName() . '.index')]; } /** * Show the edit page of the model. * * @param int $id * @return Application|Factory|View */ public function edit($id) { // get the item from the database // and store it in a variable with the name // of the model in lower case // for example, if the model is named "User" // the variable will be named "$user" ${$this->model->lowerModelName()} = $this->model::findOrFail($id); // return the edit view // with the item as a parameter // the view will be located in the folder // "admin/{lower_model_name}/edit.blade.php" return view('admin.' . $this->model->getFolderName() . '.edit', [$this->model->lowerModelName() => ${$this->model->lowerModelName()}]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return array */ public function update($request, $id) { // Check if the request is validated or not // and get the data $data = method_exists($request, 'validated') ? $request->validated() : $request->all(); // Find the item in the database // and update it with the validated // data from the request $this->model::findOrFail($id)->update($data); // check if the request is api // and return a json response if ($this->isApiRequest()) { return ['status' => 'success', 'data' => $this->apiResourceResponse($this->model::findOrFail($id))]; } // Add a log entry // using the ReportTrait // the log entry will be in the format // "edit_{lower_model_name}" // for example, if the model is named "User" // the log entry will be "edit_user" ReportTrait::addToLog(__('admin.' . $this->model->getFolderName() . '.edit')); // Return a success response return ['status' => 'success', 'url' => route('admin.' . $this->model->getFolderName() . '.index')]; } /** * Show the specified resource from storage. * * @param int $id * @return Application|Factory|View|JsonResponse */ public function show($id) { // Find the item in the database // and store it in a variable with the name // of the model in lower case // for example, if the model is named "User" // the variable will be named "$user" ${$this->model->lowerModelName()} = $this->model::findOrFail($id); // Return the show view // with the item as a parameter // the view will be located in the folder // "admin/{lower_model_name}/show.blade.php" return view('admin.' . $this->model->getFolderName() . '.show', [$this->model->lowerModelName() => ${$this->model->lowerModelName()}]); } /** * Remove the specified resource from storage. * * @param int $id * @return array */ public function destroy($id) { // Find the item in the database // and delete it $this->model::findOrFail($id)->delete(); // Add a log entry // using the ReportTrait // the log entry will be in the format // "delete_{lower_model_name}" // for example, if the model is named "User" // the log entry will be "delete_user" if (!$this->isApiRequest()) { ReportTrait::addToLog(__('admin.' . $this->model->getFolderName() . '.delete')); } // Return a success response return ['status' => 'success']; } /** * Destroy multiple items from storage. * * @return array */ public function destroyAll($request) { // Decode the json data from the request $requestIds = json_decode($request->data); // Store the ids in an array foreach ($requestIds as $id) { $ids[] = $id->id; } // Destroy all the items with the given ids // and store the result in a variable $result = $this->model::whereIn('id', $ids)->delete(); // If the result is true // add a log entry // and return a success response if ($result) { ReportTrait::addToLog(__('admin.' . $this->model->getFolderName() . '.delete_all')); return ['status' => 'success']; } else { // If the result is false // return a fail response return ['status' => 'fail']; } } /* |-------------------------------------------------------------------------- | API Functions |-------------------------------------------------------------------------- | */ public function indexApi() { // Return all the items from the database $data = $this->model::search(request()->searchArray)->paginate(request()->paginate, ['*'], 'page', request()->page); // Return a json response return $this->apiCollectionResponse($data); } /** * Show the specified resource from storage. * * @param int $id * @return JsonResponse */ public function showApi($id) { // Find the item in the database // and store it in a variable with the name // of the model in lower case // for example, if the model is named "User" // the variable will be named "$user" $data = $this->model::findOrFail($id); // Return JSON response // with the data return $this->apiResourceResponse($data); } }
Back to File Manager