<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
fadyreda99 / laravel-service-repository-maker example snippets
public function allWithCondition(array $condition = [], array $with = [], bool $paginated = false, int $limit = 10, array $orderBy = [])
{
$query = User::with($with)->where($condition);
foreach ($orderBy as $col => $dir) {
$query->orderBy($col, $dir);
}
return $paginated ? $query->paginate($limit) : $query->get();
}
public function find(int $id, array $with = [], array $condition = [])
{
return User::with($with)->where($condition)->where('id', $id)->first();
}
public function create(array $data)
{
return User::create($data);
}
public function update(int $id, array $data)
{
$record = User::findOrFail($id);
$record->update($data);
return $record;
}
public function delete(int $id, array $condition = [])
{
return (bool) User::where($condition)->where('id', $id)->delete();
}
class ReportRepository
{
// Add repository methods here
}
public function allWithCondition($request)
{
$data = $request->all();
$condition = [];
$with = [];
$paginated = false;
$limit = 10;
$orderBy = [];
return $this->repository->allWithCondition($condition, $with, $paginated, $limit, $orderBy);
}
public function find($request)
{
$data = $request->all();
$id = $data['id'];
$condition = [];
$with = [];
return $this->repository->find($id, $with, $condition);
}
public function create($request)
{
return $this->repository->create($request->all());
}
public function update($request)
{
return $this->repository->update($request->input('id'), $request->all());
}
public function delete($request)
{
$data = $request->all();
$id = $data['id'];
$condition = [];
return $this->repository->delete($id, $condition);
}
class ReportService
{
// Service methods
}
use App\Services\UserService;
class UserController extends Controller
{
public function __construct(private UserService $service) {}
public function index(Request $request)
{
return $this->service->allWithCondition($request);
}
public function store(Request $request)
{
return $this->service->create($request);
}
}