lingyun/think-repositories

Thinkphp Repositories

v1.0.2 2023-09-24 16:49 UTC

This package is auto-updated.

Last update: 2024-12-24 19:47:53 UTC


README

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.