ametsuramet/simple_orm

DB Wrapper for build simple orm in laravel

v1.2.6 2017-08-10 10:49 UTC

README

Laravel Package alternative for Eloquent ORM.

Installation

Begin by pulling in the package through Composer.

composer require ametsuramet/simple_orm:1.2.*

Or add in the require key of composer.json file manually

"ametsuramet/simple_orm": "1.2.*"

Register the ServiceProvider in config/app.php

'providers' => [
    // ...
    Amet\SimpleORM\ModelBuilderServiceProvider::class,
],

For Lumen

Add this service provider to your bootstrap/app.php file.

$app->register(Amet\SimpleORM\ModelBuilderServiceProvider::class);

Defining Models

The easiest way to create a model instance is using the simple_orm:model Artisan command:

php artisan simple_orm:model Project

or you can use interactive mode:

php artisan simple_orm:interactive

Command Options

php artisan simple_orm:model Project --soft_delete=1 --methods=transactions,users --default_key=uuid --migration=1

All models will placed in app/ORM folder.

Usage

in your controller file:

// ...

use App\ORM\Project;

class yourController extends Controller {
	
	public function yourMethod()
	{
		$projects = new Project;
		$projects = $projects->get();

		dd($projects);
	}
// ...

or something like this:

// ...
		$projects = (new Project)->get();
		dd($projects);
// ...

Table Names

By Default SimpleORM will generate $table attribut, but you can change name of table in Models

...
class Project extends BaseQuery
{
	protected $table = 'projects';
...

Default Keys

SimpleORM will also assume that each table has a primary key column named id. You may define a $default_key property to override.

...
class Project extends BaseQuery
{
	protected $default_key = 'uuid';
...

Public Method

where((array) $params)

// ...
		$projects = new Project;
		$projects = $projects->where(['id',1])->get();
		dd($projects);
// ...

or

// ...
		$wheres = [
			['id','>=',10], 
			['date','>=','2017-01-01'], 
		];
		$projects = new Project;
		$projects = $projects->where($wheres)->get();
		dd($projects);
// ...

limit((int) $params, (int) $params)

// ...
		$projects = new Project;
		$projects = $projects->limit(10)->get();
		dd($projects);
// ...

you can add offset :

// ...
		$projects = new Project;
		$projects = $projects->limit(20,10)->get();
		dd($projects);
// ...

deleted_only()

Show deleted only

// ...
		$projects = new Project;
		$projects = $projects->deleted_only()->get();
		dd($projects);
// ...

deleted((boolean) $params)

As noted above, soft deleted models will automatically be excluded from query results, you can use this method to include deleted records

// ...
		$projects = new Project;
		$projects = $projects->deleted(true)->get();
		dd($projects);
// ...

info()

// ...
		$projects = new Project;
		$projects = $projects->info();
		dd($projects);
// ...

orderBy((string) $params, (string) $params)

// ...
		$projects = new Project;
		$projects = $projects->orderBy('id')->get();
		dd($projects);
// ...

or

// ...
		$projects = new Project;
		$projects = $projects->orderBy('id','desc')->get();
		dd($projects);
// ...

groupBy()

// ...
		$projects = new Project;
		$projects = $projects->groupBy('id')->get();
		dd($projects);
// ...

or

// ...
		$projects = new Project;
		$projects = $projects->groupBy(['id','date'])->get();
		dd($projects);
// ...

insert((array) $params)

// ...
		$projects = new Project;
		$projects = $projects->insert(["name" => 'new project']);
		dd($projects);
// ...

update($id,(array) $params)

// ...
		$projects = new Project;
		$projects = $projects->update(29,["name" => 'new project']);
		dd($projects);
// ...

delete($id)

// ...
		$projects = new Project;
		$projects = $projects->delete(29);
// ...

get()

// ...
		$projects = new Project;
		$projects = $projects->get();
		dd($projects);
// ...

first()

// ...
		$projects = new Project;
		$projects = $projects->first();
		dd($projects);
// ...

find($id)

// ...
		$projects = new Project;
		$projects = $projects->find(29);
		dd($projects);
// ...

last()

// ...
		$projects = new Project;
		$projects = $projects->last();
		dd($projects);
// ...

paginate((int) $params)

// ...
		$projects = new Project;
		$projects = $projects->paginate($params);
		dd($projects);
// ...

set_show_column((array) $params)

// ...
		$projects = new Project;
		$projects = $projects->set_show_column(['id','first_name','last_name'])->get();
		dd($projects);
// ...

hide_relation()

// ...
		$projects = new Project;
		$projects = $projects->hide_relation()->get();
		dd($projects);
// ...

count()

// ...
		$projects = new Project;
		$projects = $projects->count();
		dd($projects);
// ...

getQueryLog()

// ...
		$projects = new Project;
		$projects = $projects->getQueryLog();
		dd($projects);
// ...

Relation

hasOne($relation_table,$relation_key,$table_key,$relation_name,$show_column(optional))

...
class Project extends BaseQuery
{
	public function user()
	{
		$this->hasOne(User:class,'id','user_id','user');
	}
...

with show_column

...
class Project extends BaseQuery
{
	public function user()
	{
		$this->hasOne(User:class,'id','user_id','user',['id','first_name','last_name']);
	}
...

hasMany($relation_table,$relation_key,$table_key,$relation_name,$show_column(optional))

...
class Project extends BaseQuery
{
	public function user()
	{
		$this->hasOne(User:class,'id','user_id','user');
	}
...

with show_column

...
class Project extends BaseQuery
{
	public function user()
	{
		$this->hasOne(User:class,'id','user_id','user',['id','first_name','last_name']);
	}
...

manyToMany($relation_table,$relation_key,$table_key,$relation_name,$pivot_table,$show_column(optional))

...
class Project extends BaseQuery
{
	public function user()
	{
		$pivot_table = [UserProject::class,'user_id','company_id'];
		$this->manyToMany(User:class,'id','user_id','user',$pivot_table);
	}
...

with show_column

...
class Project extends BaseQuery
{
	public function user()
	{
		$pivot_table = [UserProject::class,'user_id','company_id'];
		$this->manyToMany(User:class,'id','user_id','user',$pivot_table,['id','first_name','last_name']);
	}
...