1. Go to this page and download the library: Download omnicode/lara-repo 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/ */
omnicode / lara-repo example snippets
namespace App\Repositories\Contracts;
use LaraRepo\Contracts\RepositoryInterface;
interface AccountRepositoryInterface extends RepositoryInterface
{
}
namespace App\Repositories\Eloquent;
use LaraRepo\Eloquent\AbstractRepository;
use App\Repositories\Contracts\AccountRepositoryInterface;
use App\Models\Account;
class AccountRepository extends AbstractRepository implements AccountRepositoryInterface
{
public function modelClass()
{
return Account::class;
}
}
namespace App\Models;
use LaraModel\Models\LaraModel;
class Account extends LaraModel
{
}
namespace App\Providers;
use App\Repositories\Contracts\AccountUserRepositoryInterface;
use App\Repositories\Eloquent\AccountRepository;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(AccountUserRepositoryInterface::class, AccountRepository::class);
}
}
namespace App\Http\Controllers;
use App\Repositories\Contracts\AccountRepositoryInterface as AccountRepository;
class AccountsController extends Controller
{
/**
* @var AccountRepository
*/
protected $accountRepository;
/**
* AccountsController constructor.
* @param AccountRepository $accountRepository
*/
public function __construct(AccountRepository $accountRepository)
{
$this->accountRepository = $accountRepository;
}
/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index()
{
$items = $this->accountRepository->all();
return view('accounts.index', compact('items'));
}
}
public function makeModel();
public function getModel();
public function getModelQuery();
public function resetModelQuery();
public function getTable();
public function getKeyName();
public function fixColumns($columns, $table = null, $prefix = null);
public function getFillableColumns();
public function getIndexableColumns($full = false, $hidden = true, $group = self::GROUP);
public function getShowableColumns($full = false, $hidden = true, $group = self::GROUP);
public function getSearchableColumns();
public function getListableColumns();
public function getSortableColumns($column = null, $group = self::GROUP);
public function getStatusColumn();
public function setSortingOptions($column = null, $order = 'asc', $group = self::GROUP);
public function getRelations();
public function saveAssociated($data, $options = [], $model = null);
public function create(array $data);
public function createWith(array $data, $attribute, $value);
public function increment($column, $value);
public function update(array $data, $id, $attribute = "id");
public function updateBased(array $data, array $conditions);
public function destroy($id);
public function destroyBy($attribute, $value);
public function all($columns = null);
public function first($columns = null);
public function last($columns = null);
public function find($id, $columns = null);
public function findForShow($id, $columns = null);
public function findBy($attribute, $value, $columns = null);
public function findAllBy($attribute, $value, $columns = null);
public function findAttribute($id, $attribute);
public function findFillable($id);
public function findAllFillable($attribute, $value);
public function findFillableWith($id, $related = []);
public function findFillableWhere($id, $field, $value, $cmp = '=');
public function findList($active = true, $listable = null);
public function findListBy($attribute, $value, $active = true, $listable = null);
public function paginate($perPage = 15, $columns = null, $group = self::GROUP);
public function paginateWhere($attribute = '', $value = '', $cmp = '=');
public function findCount($attribute = null, $value = null, $cmp = '=');
public function exists($id);
public function existsWhere($attribute, $value);
}
public function getCriteria();
public function getByCriteria(Criteria $criteria);
public function resetScope();
public function pushCriteria(Criteria $criteria);
public function skipCriteria($status = true);
public function applyCriteria();
public function startTransaction();
public function commitTransaction();
public function rollbackTransaction();
$this->accountRepository->create($request->all());
$this->accountRepository->createWith($request->all(), 'name', $name);
it is equivalent
$data = array_merge($request->all(), ['name' => $name])
$this->accountRepository->create($data);
//if your model extends LaraModel/Models/LaraModel you can use saveAssociated Method for saving new account
//or update existing model with relations
$this->accountRepository->saveAssociated($data, $relations);
//for updating with relations
$this->accountRepository->saveAssociated($data, $relations, $account);
// update based on id
$this->accountRepository->update($request->all(), $accountId);
// update based on attribute
$this->accountRepository->update($request->all(), $attribute, 'attribute');
// for increment attribute with value
$this->accountRepository->increment($attribute, $value);
//for decrement attribute with value
$this->accountRepository->decrement($attribute, $value);
// delete by id
$this->accountRepository->destroy($accountId);
// find by accountId
$this->accountRepository->find($accountId, $columns);
// find by attribute (first occurence)
$this->accountRepository->findBy($attribute, $value, $columns);
// find by attribute (all occurence)
$this->accountRepository->findAllBy($attribute, $value, $columns);
// to find and return exact attribute
$this->accountRepository->findAttribute($accountId, $attribute);
// to find account by accountId with fillable columns
$this->accountRepository->findFillable($accountId);
// to find account by attribute with fillable columns
$this->accountRepository->findFillableWhere($id, $attribute, $value, $cmp = '=')
// to find all accounts by attribute with fillable columns use
$this->accountRepository->findAllFillable($attribute, $value);
// to find account with relations
$this->accountRepository->findFillableWith($id, $related = [])
// to find all accounts
$this->accountRepository->all($columns)
// to find first account
$this->accountRepository->first($columns = null)
//for find last account
$this->accountRepository->last($columns = null)
// if your model extends LaraModel\Models\LaraModel
// and $showable property is specified you can use
$this->accountRepository->findForShow($id, $columns = null)
// if your model extends LaraModel\Models\LaraModel
// and specifed $listable property you can use
// for find accounts list
//[
// 1 => 'account1',
// 2 => 'account2'
//]
//
$this->accountRepository->findList($active = true, $listable = null)
// or you can use to find by attribute
$this->accountRepository->findListBy($attribute, $value, $active = true, $listable = null)
// to check if account exists with id
$this->accountRepository->exists($id);
// to find count of rows matching the given critera
$this->accountRepository->findCount($attribute, $value, $cmp);
// to check if item exists with given attribute and value
$this->accountRepository->existsWhere($attribute, $value);
// for pagination
$this->accountRepository->paginate();
// for pagination based by condition
$this->accountRepository->paginateWhere($attribute, $value);
// to get the binded model
$this->account->getModel();
// get current model query
$this->account->getModelQuery();
// to reset model query
$this->account->resetModelQuery();
// to get model's table name
$this->account->getTable();
// to get model table's primary key
$this->account->getKeyName();
// to get model's fillable columns
$this->account->getFillableColumns();