81 lines
1.7 KiB
PHP
81 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace Modules\Client\Repositories;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Modules\Client\Interfaces\ClientInterface;
|
|
use Modules\Client\Models\Client;
|
|
|
|
class ClientRepository implements ClientInterface
|
|
{
|
|
public function findAll($request, callable $query = null, bool $paginate = false, int $limit = 10)
|
|
{
|
|
$baseQuery = Client::query();
|
|
|
|
if ($request->filled('search')) {
|
|
$baseQuery->whereAny(
|
|
[
|
|
'name',
|
|
'manager_name',
|
|
'poc_name',
|
|
],
|
|
'LIKE',
|
|
"%{$request->search}%"
|
|
);
|
|
}
|
|
|
|
if ($query) {
|
|
$query($baseQuery);
|
|
}
|
|
|
|
if ($paginate) {
|
|
return $baseQuery->paginate($limit);
|
|
}
|
|
|
|
return $baseQuery->get();
|
|
}
|
|
|
|
public function findById($id, callable $query = null)
|
|
{
|
|
$baseQuery = Client::query();
|
|
|
|
if (is_callable($query)) {
|
|
$query($baseQuery);
|
|
}
|
|
|
|
return $baseQuery->where('id', $id)->firstOrFail();
|
|
|
|
}
|
|
|
|
public function delete($id)
|
|
{
|
|
$client = $this->findById($id);
|
|
$client->delete();
|
|
return $client;
|
|
}
|
|
|
|
public function create(array $data)
|
|
{
|
|
$client = Client::create($data);
|
|
return $client;
|
|
}
|
|
|
|
public function update($id, array $data)
|
|
{
|
|
$client = $this->findById($id);
|
|
$client->update($data);
|
|
return $client;
|
|
}
|
|
|
|
public function pluck(callable $query = null)
|
|
{
|
|
$baseQuery = Client::query();
|
|
|
|
if (is_callable($query)) {
|
|
$query($baseQuery);
|
|
}
|
|
|
|
return $baseQuery->pluck('name', 'id');
|
|
}
|
|
}
|