Skip to content

think-Repositories is a package for Thinkphp 6.0 which is used to abstract the database layer. This makes applications much easier to maintain.

Notifications You must be signed in to change notification settings

ouyanglingyun/think-repositories

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Thinkphp Repositories

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

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.

About

think-Repositories is a package for Thinkphp 6.0 which is used to abstract the database layer. This makes applications much easier to maintain.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages