BaseService
Overview
The BaseService class is an abstract service layer class designed to provide a standardized set of CRUD and utility operations for Eloquent models in a Laravel application. It acts as an intermediary between controllers and repositories, encapsulating business logic and delegating data access to a corresponding repository.
This class is intended to be extended by concrete service classes for specific models. It relies on a repository class (implementing BaseRepository) to perform actual data operations.
Properties
- protected BaseRepository $repository: The repository instance used for data operations.
- protected string $repositoryClass: The class name of the repository to instantiate (defaults to
BaseRepository::class).
Constructor
- protected function __construct()
- Instantiates the repository using the class specified in
$repositoryClass.
- Instantiates the repository using the class specified in
Methods
delete
public function delete($id): ?boolDeletes a record by its ID.
- Parameters:
$id: The primary key of the record to delete.
- Returns:
bool|null—trueif deleted,falseif not, ornullif not found.
index
public function index(array $relations = []): Collection|array|RegularCollectionRetrieves all records, optionally eager loading relationships.
- Parameters:
$relations: Array of relationship names to eager load.
- Returns:
Collection,array, orRegularCollectionof models.
indexWithPagination
public function indexWithPagination(array $relations = [], int $per_page = 10): LengthAwarePaginatorRetrieves records with pagination.
- Parameters:
$relations: Array of relationship names to eager load.$per_page: Number of records per page (default: 10).
- Returns:
LengthAwarePaginatorof models.
store
public function store(array $data, array $relationships = []): ModelCreates a new record.
- Parameters:
$data: Array of attributes for the new record.$relationships: Array of related data to associate.
- Returns: The created
Modelinstance.
update
public function update(array $data, $id, array $relationships = []): ?ModelUpdates an existing record by ID.
- Parameters:
$data: Array of attributes to update.$id: The primary key of the record to update.$relationships: Array of related data to update.
- Returns: The updated
Modelinstance ornullif not found.
view
public function view($id, array $relationships = []): ?ModelRetrieves a single record by ID, optionally eager loading relationships.
- Parameters:
$id: The primary key of the record to retrieve.$relationships: Array of relationship names to eager load.
- Returns: The
Modelinstance ornullif not found.
export
public function export(array $ids = []): BinaryFileResponseExports records (optionally filtered by IDs) to a file (e.g., Excel or CSV).
- Parameters:
$ids: Array of record IDs to export (optional).
- Returns:
BinaryFileResponsefor file download. - Throws:
Exceptionon failure.
getImportExample
public function getImportExample(): BinaryFileResponseProvides an example file for data import (e.g., Excel template).
- Returns:
BinaryFileResponsefor file download. - Throws:
Exception,\PhpOffice\PhpSpreadsheet\Writer\Exceptionon failure.
import
public function import(): voidImports data from a file (implementation in repository).
- Returns:
void
Usage Example
To use BaseService, extend it in your own service class and specify the repository class:
use App\Services\Contracts\BaseService;
use App\Repositories\UserRepository;
/**
* @extends BaseService<User>
* @property UserRepository $repository
*/
class UserService extends BaseService
{
protected string $repositoryClass = UserRepository::class;
// Add custom business logic here
}Notes
- This class is abstract and should not be instantiated directly.
- All data operations are delegated to the repository specified by
$repositoryClass. - Extend this class to implement model-specific business logic in your application.
