Skip to content

Instantly share code, notes, and snippets.

@tperrelli
Created June 3, 2021 13:59
Show Gist options
  • Save tperrelli/eb13cf1540066e93ce305514964e9d6a to your computer and use it in GitHub Desktop.
Save tperrelli/eb13cf1540066e93ce305514964e9d6a to your computer and use it in GitHub Desktop.
Base repository
<?php
namespace App\YourNamespace;
use Illuminate\Database\Eloquent\Builder as EloquentQueryBuilder;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Pagination\AbstractPaginator as Paginator;
abstract class BaseRepository
{
/**
* Model class for repo.
*
* @var string
*/
protected $modelClass;
/**
* @return EloquentQueryBuilder|QueryBuilder
*/
protected function newQuery()
{
return app($this->modelClass)->newQuery();
}
/**
* @param EloquentQueryBuilder|QueryBuilder $query
* @param int $take
* @param bool $paginate
*
* @return EloquentCollection|Paginator
*/
protected function doQuery($query = null, $take = 15, $paginate = true)
{
if (is_null($query)) {
$query = $this->newQuery();
}
if (true == $paginate) {
return $query->paginate($take);
}
if ($take > 0 || false !== $take) {
$query->take($take);
}
return $query->get();
}
/**
* Returns all records.
* If $take is false then brings all records
* If $paginate is true returns Paginator instance.
*
* @param int $take
* @param bool $paginate
*
* @return EloquentCollection|Paginator
*/
public function getAll($take = 15, $paginate = true)
{
return $this->doQuery(null, $take, $paginate);
}
/**
* @param string $column
* @param string|null $key
*
* @return \Illuminate\Support\Collection
*/
public function lists($column, $key = null)
{
return $this->newQuery()->lists($column, $key);
}
/**
* Retrieves a record by his id
* If fail is true $ fires ModelNotFoundException.
*
* @param int $id
* @param bool $fail
*
* @return Model
*/
public function findByID($id, $fail = true)
{
if ($fail) {
return $this->newQuery()->findOrFail($id);
}
return $this->newQuery()->find($id);
}
}
<?php
namespace App\YourNamespace;
use App\Models\Order;
use App\Repositories\RegularProviderRepository;
class OrderRepository extends BaseRepository implements OrderRepositoryInterface
{
use RegularProviderRepository;
protected $modelClass = Order::class;
public function getOrderInvoices(Order $order, $take = 30, $paginate = false)
{
$query = $order->invoices()->getQuery();
$query->orderBy('created_at', 'DESC');
$query->with('orderDetails');
return $this->doQuery($query, $take, $paginate);
}
}
<?php
namespace App\YourNamespace;
interface UserRepositoryInterfafe {};
<?php
namespace App\YourNamespace;
use App\Models\ProviderOrg;
/**
* This trait would act as a super class to filter children classes by provider_org_id
* It could be re used in many other children classes also
*/
trait RegularProviderRepository
{
/**
* @var ProviderOrg
*/
protected $provider;
public function __construct(ProviderOrg $tenant)
{
$this->tenant = $tenant;
}
/**
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
*/
public function newQuery()
{
$query = parent::newQuery();
return $query->where('provider_org_id', $this->tenant->id);
}
/**
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function factory(array $data = [])
{
$model = parent::factory($data);
$model->provider_org_idi = $this->tenant->id;
return $model;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment