PHP code example of zhaohehe / zrepository

1. Go to this page and download the library: Download zhaohehe/zrepository 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/ */

    

zhaohehe / zrepository example snippets


 namespace App\Repositories;

use Zhaohehe\Repositories\Eloquent\Repository;

class PoemRepository extends Repository 
{

    public function model() 
    {
        return 'App\Models\Poem';
    }
}

 namespace App;

use Illuminate\Database\Eloquent\Model;

class Poem extends Model 
{

}

 namespace App\Http\Controllers;

use App\Repositories\PoemRepository as Poem;

class PoemController extends Controller 
{

    protected $poem;

    public function __construct(Poem $poem) 
    {

        $this->poem = $poem;
    }

    public function index() 
    {
        return $this->poem->all();
    }
}

public function all($columns = ['*']);    //获取所有记录

public function paginate($perPage = 15, $columns = ['*']);    //分页,默认每页15条

public function create(array $data);    //创建一条记录

public function save(array $data);    //保存

public function delete($id);    //删除一条记录

public function update(array $data, $id);    //更新记录

public function find($id, $columns = ['*']);    //按id查找

public function findBy($field, $value, $columns = ['*']);    //按指定字段查找

public function findWhere($where, $columns = ['*']);    //按多个条件查找

$this->Poem->create(Input::all());

$this->Poem->update(Input::all(), $id);

$this->poem->delete($id);

$this->poem->find($id);

$this->poem->find($id, ['title', 'description', 'author']);

$this->poem->findBy('title', $title);

$this->poem->findWhere([
    'author' => $author_id,
    ['year','>',$year]
]);

 namespace App\Repositories\Criteria\Poem;

use Zhaohehe\Repositories\Criteria\Criteria;
use Zhaohehe\Repositories\Contracts\RepositoryInterface as Repository;

class CreatedInTangDynasty extends Criteria 
{

    public function apply($model, Repository $repository)
    {
        $model = $model->where('dynasty', '=', '唐朝');
        return $model;
    }
}

 namespace App\Http\Controllers;

use App\Repositories\Criteria\CreatedInTangDynasty;
use App\Repositories\PoemRepository as Poem;

class PoemController extends Controller 
{
    private $poem;

    public function __construct(Poem $poem) 
    {
        $this->poem = $poem;
    }

    public function index() 
    {
        $this->poem->pushCriteria(new CreatedInTangDynasty());
        return $this->poem->all();
    }
}

use App\Models\Poem;
use League\Fractal\TransformerAbstract;

class PoemTransformer extends TransformerAbstract
{
    public function transform(Poem $model)
    {
        return [
            'id'      => (int) $model->id,
            'title'   => $model->title,
            'author'  => $model->author->name
        ];
    }
}

namespace App\Repositories;
use Zhaohehe\Repositories\Eloquent\Repository;

class PoemRepository extends Repository
{
    public function model()
    {
        return 'App\Poem';
    }
    
    public function transfomer()
    {
        return "App\\Transformers\\PoemTransformer";
    }
}

$this->poem->setTransformer("App\\Transformers\\PoemTransformer");



namespace App;

use Illuminate\Database\Eloquent\Model;
use Zhaohehe\Repositories\Contracts\Transformable;
use Zhaohehe\Repositories\Traits\TransformableTraits;

class Poem extends Model implements Transformable
{
    use TransformableTraits;
}


$repository = app('App\Repositories\PoemRepository');
$repository->setTransformer("App\\Transformers\\PoemTransformerr");

$poem = $repository->find(1);    //获取被转换过的查询结果

dd( $poem );    //这会返回一个按照Poemtransformer定义过的数组

...

$poem = $repository->skipTransformer()->find(1);    //跳过transformer,返回原始的数据模型查询结果

dd( $poem );    //这会返回一个普通model的查询结果
dd( $posem->transform() );    //调用$poem的



namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Zhaohehe\Repositories\Traits\ModelEventTraits;
use Zhaohehe\Repositories\Contracts\ModelEventInterface;

class Poem extends Model implements ModelEventInterface
{
    use ModelEventTraits;

    protected $fillable = [
   
    ];

}



namespace App\Repositories;

use Zhaohehe\Repositories\Eloquent\Repository;

class PoemRepository extends Repository
{
    public function model()
    {
        return 'App\Models\Poem';
    }

    public function onCreated()
    {
        //do something
    }
}

function onCreating();
function onCreated();

function onUpdating();
function onUpdated();

function onSaving();
function onSaved();

function onDeleting();
function onDeleted();
bash
php artisan make:repository Poem --model Poem
bash
php artisan make:transformer Poem