1. Go to this page and download the library: Download tricioandrade/oneshot library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tricioandrade / oneshot example snippets
namespace App\Enums;
enum EmployeeFunctions
{
/**
* Return all cases values as array
*
* @return array
*/
public function values(): array
{
return array_column(self::cases(), 'values');
}
}
namespace App\Services;
use App\Exceptions\Auth\UnauthorizedException;
use App\Models\Transport\FuelSupplyModel;
use App\Traits\Essentials\Database\CrudTrait;
use App\Traits\Essentials\VerifyTypeUserTrait;
use Illuminate\Database\Eloquent\Collection;
class FuelSupplyService
{
use CrudTrait, VerifyTypeUserTrait;
public function __construct()
{
$this->relations = [];
$this->model = new FuelSupplyModel();
}
/**
* Get all data from the database
*
* @throws UnauthorizedException
*/
public function getAll(): FuelSupplyModel|Collection
{
if (!$this->verifyAdmin()) throw new UnauthorizedException();
return $this->getAllData();
}
/**
* Create a new data in the database
*
* @throws UnauthorizedException
*/
public function create(array $attributes) {
if (!$this->verifyAdmin()) throw new UnauthorizedException();
return $this->createData($attributes);
}
/**
* Get a data from the database by id
*
* @param int $id
* @return FuelSupplyModel|Collection
* @throws UnauthorizedException
*/
public function getById(int $id): FuelSupplyModel|Collection
{
if (!$this->verifyAdmin()) throw new UnauthorizedException();
return $this->getByIdentity($id);
}
/**
* Update a specific data in the database
*
* @param array $attributes
* @param int $id
* @return FuelSupplyModel|Collection
* @throws UnauthorizedException
*/
public function update(array $attributes, int $id): FuelSupplyModel|Collection
{
if (!$this->verifyAdmin()) throw new UnauthorizedException();
return $this->updateData($attributes, $id);
}
/**
* Trash a specified data in the database
*
* @param int $id
* @return mixed
* @throws UnauthorizedException
*/
public function delete(int $id): mixed
{
if (!$this->verifyAdmin()) throw new UnauthorizedException();
return $this->deleteData($id);
}
/**
* Permanently delete a specific data in the database
*
* @param int $id
* @return mixed
* @throws UnauthorizedException
*/
public function forceDelete(int $id): mixed
{
if (!$this->verifyAdmin()) throw new UnauthorizedException();
return $this->forceDeleteData($id);
}
/**
* Restore a specific data in the database
*
* @param int $id
* @return mixed
* @throws UnauthorizedException
*/
public function restore(int $id): mixed
{
if (!$this->verifyAdmin()) throw new UnauthorizedException();
return $this->restoreData($id);
}
}
namespace App\Http\Controllers\User;
use App\Http\Controllers\Controller;
use App\Http\Requests\User\EmployeeRequest;
use App\Http\Resources\User\EmployeeResource;
use App\Services\User\EmployeeService;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
class EmployeeController extends Controller
{
public function __construct(
public EmployeeService $employeeService
){}
/**
* Display a listing of the resource.
*
* @return AnonymousResourceCollection
* @throws UnauthorizedException
*/
public function index(): AnonymousResourceCollection
{
return EmployeeResource::collection($this->employeeService->getAll());
}
/**
* Store a newly created resource in storage.
*
* @param EmployeeRequest $employeeRequest
* @return EmployeeResource
* @throws UnauthorizedException
*/
public function store(EmployeeRequest $employeeRequest): EmployeeResource
{
$employeeRequest->validated($employeeRequest->all());
$employee = $this->employeeService->create($employeeRequest->all());
return new EmployeeResource($employee);
}
/**
* Display the specified resource.
*
* @param int $id
* @return EmployeeResource
* @throws UnauthorizedException
*/
public function show(int $id): EmployeeResource
{
$employee = $this->employeeService->getById($id);
return new EmployeeResource($employee);
}
/**
* Update the specified resource in storage.
*
* @param EmployeeRequest $employeeRequest
* @param int $id
* @return EmployeeResource
* @throws UnauthorizedException
*/
public function update(EmployeeRequest $employeeRequest, int $id): EmployeeResource
{
$employeeRequest->validated($employeeRequest->all());
$employee = $this->employeeService->getById($id);
return new EmployeeResource($employee);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return mixed
* @throws UnauthorizedException
*/
public function destroy(int $id): mixed
{
return $this->employeeService->delete($id);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return mixed
* @throws UnauthorizedException
*/
public function forceDelete(int $id): mixed
{
return $this->employeeService->forceDelete($id);
}
/**
* Restore the specified resource from storage.
*
* @param int $id
* @return mixed
* @throws UnauthorizedException
*/
public function restore(int $id): mixed
{
return $this->employeeService->restore($id);
}
}
/**
* From OneShot v3
* @link https://github.com/tricioandrade/oneshot
*/
namespace App\Traits\Essentials\Database;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
trait CrudTrait
{
private array | string $relations;
private Model | Builder $model;
/**
* Get all data
*
* @return mixed
*/
private function getAllData(): mixed
{
return $this->model::withTrashed()->with($this->relations)->get();
}
/**
* Get data by Id
*
* @param int $id
* @return mixed
*/
private function getByIdentity( int $id): mixed
{
return $this->model::withTrashed()->with($this->relations)->findOrFail($id);
}
/**
* Get data by slug
*
* @param string $slug
* @return mixed
*/
private function getBySlugInfo(string $slug): mixed
{
return $this->model::withTrashed()->with($this->relations)->where('slug', $slug)->get();
}
/**
* Get data by anonymous row
*
* @param string $anonymousRow
* @param $value
* @return mixed
*/
private function getByAnonymousInfo(string $anonymousRow, $value): mixed
{
return $this->model::withTrashed()->with($this->relations)->where($anonymousRow, $value)->get();
}
/**
* Create data
*
* @param array $attributes
* @return mixed
*/
private function createData(array $attributes): mixed
{
$create = $this->model::create($attributes);
return $create->load($this->relations);
}
/**
* Update data
*
* @param int $id
* @param array $attributes
* @return mixed
*/
private function updateData(array $attributes, int $id): mixed
{
$update = $this->getByIdentity($id);
$update->update($attributes);
return $update->load($this->relations);
}
/**
* Delete data | put on trash
*
* @param int $id
* @return mixed
*/
private function deleteData(int $id): mixed
{
$target = $this->getByIdentity($id);
return $target->delete($id);
}
/**
* Delete data permanently
*
* @param int $id
* @return mixed
*/
private function forceDeleteData(int $id): mixed
{
$target = $this->getByIdentity($id);
return $target->forceDelete($id);
}
/**
* Restore data from database
*
* @param int $id
* @return mixed
*/
private function restoreData(int $id): mixed
{
$target = $this->getByIdentity($id);
return $target->restore($id);
}
}
php artisan make:enum EmployeeFunctions
php artisan make:trait EmployeeFunctions
php artisan make:service EmployeeFunctionsService
php artisan make:api-resources User/Employee
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.