Backend Development 4 min read

ThinkPHP 6 Repository Package (think-repository) Usage Guide

This guide introduces the think-repository package for ThinkPHP 6, explains how to install it via Composer, demonstrates common repository methods for CRUD operations, routing, and transformer generation, and provides code examples for integrating the repository into multi‑application back‑end projects.

php中文网 Courses
php中文网 Courses
php中文网 Courses
ThinkPHP 6 Repository Package (think-repository) Usage Guide

Introduction

The think-repository package provides an abstract data‑layer repository for ThinkPHP 6.x, making applications easier to maintain and extend.

Installation

Install the package with Composer:

composer require fanxd/think-repository dev-master

Usage

After installation, you can generate a repository command:

php think fanxd:repository Post

Define a resource route for the generated controller:

Route::resource('post', 'PostController');

Available Repository Methods

first($id) – find a single record by primary key

get() – retrieve records

paginate() – paginate query results

create($data) – insert new data

save($data) – save the current model instance

delete($where) – delete records

update($where, $data) – update records

find($id) – find a single record or throw an exception

findWhere($where, $columns = ['*']) – find a single record with AND conditions

with([]) – eager‑load relationships

search([]) – perform data search

order($order) – apply ordering

Common Queries

Retrieve all records:

$posts = $this->repository->get();

Paginate results:

$posts = $this->repository->paginate($limit);

Find by ID:

$posts = $this->repository->find($id);
$posts = $this->repository->first($id);

Eager‑load a relationship:

$posts = $this->repository->with(['state'])->find($id);

Find by a specific field:

$posts = $this->repository->findByField('title', 'Hello');

Find with multiple conditions:

$posts = $this->repository->findWhere(['id' => 1], ['id', 'title']);

Find where the ID is in a set:

$posts = $this->repository->findWhereIn('id', [1,2,3,4,5]);

Find where the ID is not in a set:

$posts = $this->repository->findWhereNotIn('id', [6,7,8,9,10]);

Data Manipulation

Create a new record:

$post = $this->repository->create($data);

Update existing records:

$posts = $this->repository->update($where, $data);

Delete a record by ID:

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

Delete records matching multiple fields:

$this->repository->deleteWhere(['id' => 1, 'user_id' => 1]);

Transformer

The system can automatically generate a transformer class to format API responses. You may enable it as needed.

<?php
namespace app\api\transform;
use fanxd\repository\command\transform\Transform;
class PostTransform extends Transform {
    public function transform($items)
    {
        return [
            'id' => $items['id'],
            // ... other fields ...
            'createTime' => $items['create_time'],
            'updateTime' => $items['update_time'],
        ];
    }
}

Additional methods will be added continuously; contributions are welcome.

CRUDComposerRepository PatternThinkPHP
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.