lingyun / think-repositories
Thinkphp Repositories
Installs: 16
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Type:think-extend
Requires
- topthink/framework: ^6.0||^8.0
This package is auto-updated.
Last update: 2024-12-24 19:47:53 UTC
README
think-repositories is a package for Thinkphp 6.0 which is used to abstract the database layer. This makes applications much easier to maintain.
Installation
Run the following command from you terminal:
composer require think/repositories
Usage
First, create your repository class. Note that your repository class MUST extend think\Repository
and implement model() method
<?php namespace app\repositories; use think\Repository; class FilmsRepository extends Repository { public function model() { return 'App\Film'; } }
It can be created using the make:R
command:
php think make:R admin@data/City common@data/DataCity --E=common@BaseRepository
By implementing model()
method you telling repository what model class you want to use. Now, create App\Film
model:
<?php namespace app\model; use think\Model; class Film extends Model { protected $primaryKey = 'film_id'; protected $table = 'film'; protected $casts = [ "rental_rate" => 'float' ]; }
And finally, use the repository in the controller:
<?php namespace app\controller; use app\repositories\FilmsRepository as Film; class FilmsController extends Controller { private $film; public function __construct(Film $film) { $this->film = $film; } public function index() { return \Response::json($this->film->all()); } }
Thanks
This package is largely inspired by this great package by @bosnadev.