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 = ['*']); //按多个条件查找
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";
}
}
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;
}
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();