2017 © Pedro Peláez
 

library crud-service

Eloquent model-independent CRUD Service

image

budgetdumpster/crud-service

Eloquent model-independent CRUD Service

  • Tuesday, May 22, 2018
  • by mfrost503
  • Repository
  • 0 Watchers
  • 0 Stars
  • 115 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 11 Versions
  • 98 % Grown

The README.md

CrudService

Installation

composer require budgetdumpster/crud-service:dev-master, (*1)

Tests

This package comes with a full test suite, and that test suite can be run by executing the command phpunit in the package root directory., (*2)

Usage

The CrudService package was designed to work with Eloquent Models (Laravel) and creates an unspecific service that will allow you to perform CRUD operations on your data and models., (*3)

Retrieve Single Model

<?php

use \BudgetDumpster\Services\CRUDService;
use \BudgetDumpster\Exceptions\ModelNotFoundException;
use \YourNameSpace\Models\Person;
use \Monolog\Logger;

$id = 'abcdef123456789';
$logger = new Logger('test');
// continue configuring monolog

$crudService = new CRUDService($logger);
$person = new Person();

try {
    model = $crudService->retrieve($person, $id);
} catch (ModelNotFoundException $e) {
   // log the error
}

This is a base use case for retrieving a single model by id, there's a built in way of Eager Loading the relationships, which is simply to provide a public property on the model that is called relationNames. The relation names are just strings that match the name of the relationship methods in the model. Relationships can be loaded in a normal manner by referring to that property, the relationNames property only comes into play if the relationships need to be eager loaded., (*4)

Create Single Model

<?php 

use \BudgetDumpster\Services\CRUDService;
use \BudgetDumpster\Exceptions\ModelNotFoundException;
use \YourNameSpace\Models\Person;
use \Monolog\Logger;
use \RuntimeException;

$data = [
    'first_name' => 'Test',
    'last_name' => 'Person',
    'phone' => '555-555-5555',
    'email' => 'test@test.com'
];

$logger = new Logger('test');
//continue configuring logger

$crudService = new CrudService($logger);
$person = new Person();
$id = 'abcdef123456789';

try {
    $model = $crudService->create($person, $data, $id);
} catch (RuntimeException $e) {
    // log error
}

Update Single Model

<?php
use \BudgetDumpster\Services\CRUDService;
use \BudgetDumpster\Exceptions\ModelNotFoundException;
use \YourNameSpace\Models\Person;
use \Monolog\Logger;
use \RuntimeException;

$data = [
    'first_name' => 'Test',
    'last_name' => 'Person',
    'phone' => '555-555-5554',
    'email' => 'test@test.com'
];

$logger = new Logger('test');
// continue configuring logger

$crudService = new CRUDService($logger);
$person = new Person;
$id = '123456789abcdef';

try {
    $model = $crudService->update($person, $data, $id);
} catch (ModelNotFoundException $e) {
    // log error
} catch (RuntimeException $e) {
    // log error
}

Delete Single Model

<?php
use \BudgetDumpster\Services\CRUDService;
use \BudgetDumpster\Exceptions\ModelNotFoundException;
use \YourNameSpace\Models\Person;
use \Monolog\Logger;
use \RuntimeException;

$logger = new Logger('test');
// continue configuring logger

$crudService = new CRUDService($logger);
$person = new Person;
$id = '123456789abcdef';

try {
    // returns a boolean value to identify whether the method call was successful
    $result = $crudService->delete($person, $id);
} catch (ModelNotFoundException $e) {
    // log error
} catch (RuntimeException $e) {
    // log error
}

Retrieve Collection of Models

<?php
use \BudgetDumpster\Services\CRUDService;
use \BudgetDumpster\Exceptions\ModelNotFoundException;
use \YourNameSpace\Models\Person;
use \Monolog\Logger;
use \RuntimeException;
use \InvalidArgumentException;

$logger = new Logger('test');
// continue configuring logger

$crudService = new CRUDService($logger);
$person = new Person;
$page = 1;
$per_page = 1;
// optional parameter - defaults to id != null
$filter = ['field' => 'first_name', 'operator' => '=', 'value' => 'Test'];

try {
    $collection = $crudService->retrieveAll($person, $page, $per_page, $filter);
} catch (InvalidArgumentException $e) {
    // log error
} catch (RuntimeException $e) {
    // log error
}

The Versions

22/05 2018

1.1.0

1.1.0.0 https://github.com/BD-StrategicTech/CrudService

Eloquent model-independent CRUD Service

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent crud

09/05 2018

dev-master

9999999-dev https://github.com/BD-StrategicTech/CrudService

Eloquent model-independent CRUD Service

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent crud

09/05 2018

1.0.1

1.0.1.0 https://github.com/BD-StrategicTech/CrudService

Eloquent model-independent CRUD Service

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent crud

07/05 2018

1.0.0

1.0.0.0 https://github.com/BD-StrategicTech/CrudService

Eloquent model-independent CRUD Service

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent crud

07/05 2018

0.1.6

0.1.6.0 https://github.com/BD-StrategicTech/CrudService

Eloquent model-independent CRUD Service

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent crud

06/04 2018

0.1.5

0.1.5.0 https://github.com/BD-StrategicTech/CrudService

Eloquent model-independent CRUD Service

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent crud

06/04 2018

0.1.4

0.1.4.0 https://github.com/BD-StrategicTech/CrudService

Eloquent model-independent CRUD Service

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent crud

05/04 2018

0.1.3

0.1.3.0 https://github.com/BD-StrategicTech/CrudService

Eloquent model-independent CRUD Service

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent crud

08/02 2018

0.1.2

0.1.2.0 https://github.com/BD-StrategicTech/CrudService

Eloquent model-independent CRUD Service

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent crud

08/02 2018

0.1.1

0.1.1.0 https://github.com/BD-StrategicTech/CrudService

Eloquent model-independent CRUD Service

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent crud

07/02 2018

0.1

0.1.0.0 https://github.com/BD-StrategicTech/CrudService

Eloquent model-independent CRUD Service

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel eloquent crud