1. Go to this page and download the library: Download usman/make-services 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/ */
namespace App\Services;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
class UserService
{
public function getAll(): Collection
{
return User::all();
}
public function findById(int $id): ?User
{
return User::find($id);
}
public function create(array $data): User
{
return User::create($data);
}
// ... more CRUD methods
}
namespace App\DataTransferObjects;
class UserData
{
public function __construct(
public string $name,
public string $email,
public int $age,
) {
}
public function toArray(): array
{
return [
'name' => $this->name,
'email' => $this->email,
'age' => $this->age,
];
}
public static function fromArray(array $data): static
{
return new static(
$data['name'] ?? null,
$data['email'] ?? null,
$data['age'] ?? null,
);
}
}
namespace App\Enums;
enum UserRole: string
{
case ADMIN = 'admin';
case EDITOR = 'editor';
case VIEWER = 'viewer';
public static function values(): array
{
return array_column(self::cases(), 'value');
}
public static function options(): array
{
// Perfect for select inputs
}
}
// In App\Providers\AppServiceProvider::register()
$this->app->bind(
\App\Interfaces\UserServiceInterface::class,
\App\Services\UserService::class
);