1. Go to this page and download the library: Download vxsoft/laravel-repository 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/ */
vxsoft / laravel-repository example snippets
namespace App\Http\Repositories;
use App\Models\User;
use Vxsoft\LaravelRepository\Repository;
use Illuminate\Support\Collection;
/**
* UserRepository
*
* Provides an abstraction layer for interacting with the User model,
* extending the base Repository class to leverage CRUD operations and allow
* for custom query logic.
*
* @extends Repository<User>
*/
class UserRepository extends Repository
{
/**
* Constructor binds the repository to the User model.
*/
public function __construct()
{
parent::__construct(User::class);
}
// Custom repository logic can be added here
}
public function getUserList(array $filters = []): mixed
{
$qb = $this->model->select('*');
if (!empty($filters['status'])) {
$qb->where('status', $filters['status']);
}
return $qb;
}
public function __construct(private readonly UserRepository $userRepository)
{
}
/**
* User List API
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function index(Request $request)
{
$users = $this->userRepository->getUserList($request->all())->get();
return response()->json([
'success' => true,
'message' => 'List all users',
'data' => UserResource::collection($users),
]);
}
public function __construct(private readonly EntityManagerInterface $entityManager)
{
}
/**
* User List API
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function index(Request $request)
{
$users = $this->entityManager
->getRepository(User::class)
->getUserList($request->all())
->get();
return response()->json([
'success' => true,
'message' => 'List all users',
'data' => UserResource::collection($users),
]);
}
/**
* Get the current model instance.
*
* This method returns the model associated with the repository.
* The returned object could either be an instance of the Model class or any object that extends Model.
* Useful when you need to access the model for further operations in the repository.
*
* @return Model|mixed The model instance being used by the repository.
*/
public function getModel();
/**
* Get the name of the table associated with the model.
*
* This method retrieves the table name from the model instance.
* It assumes that the model has a method `getTable()` which returns the database table's name associated with the model.
* Useful when you need to dynamically reference the model's table in queries.
*
* @return string The table name associated with the current model.
*/
public function getTable();
/**
* Create a new query builder instance for the current table.
*
* This method initiates a query builder for the table associated with the current model.
* It uses the `connection` property to access the database connection and starts a new query on the table retrieved from `getTable()`.
* Useful when you need to build complex queries programmatically in the repository.
*
* @return \Illuminate\Database\Query\Builder A new query builder instance for the table.
*/
public function createQueryBuilder();
/**
* Find a single record based on criteria.
* @param array $criteria
* @return Model|array|Builder|null
*/
public function findOneBy(array $criteria): Model|array|Builder|null;
/**
* Return all records.
* @return mixed|Collection|null
*/
public function findAll(): mixed;
/**
* Return records based on filters and ordering.
* @param array $filters
* @param array $orders
* @return mixed
*/
public function findBy(array $filters, array $orders = []): mixed;
/**
* Retrieve a record by its ID.
* @param $id
* @return mixed
*/
public function getById($id): mixed;
/**
* Create a new record with the provided data.
* @param array $data
* @return mixed
*/
public function create(array $data): mixed;
/**
* Update an existing record by ID.
* @param $id
* @param array $data
* @return mixed
*/
public function update($id, array $data): mixed;
/**
* Delete a record by ID.
* @param $id
* @return void
*/
public function delete($id): void;
/**
* Execute a raw SQL query with optional bindings.
* @param string $sql
* @param array $bindings
* @param bool $isSelect
* @return array|bool
*/
public function nativeQuery(string $sql, array $bindings = [], bool $isSelect = true): array|bool;
/**
* Persist the entity.
* @param Model $entity
* @return void
*/
public function persist(Model $entity): void;
/**
* Commit the transaction.
* @return void
*/
public function flush(): void;
/**
* Get the database connection.
* @return Connection
*/
public function getConnection(): Connection;
/**
* Begin a database transaction.
* @return void
* @throws \Throwable
*/
public function beginTransaction(): void;
/**
* Commit the database transaction.
* @return void
* @throws \Throwable
*/
public function commit(): void;
/**
* Rollback the transaction.
* @return void
* @throws \Throwable
*/
public function rollback(): void;