Coder Social home page Coder Social logo

bsormagec / laravel-repository Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wanchaochao/laravel-repository

0.0 0.0 0.0 3.05 MB

laravel repository, 基于Eloquent Model二次封装, 抛弃传统的链式调用, 让代码更简洁, 使用更直观的数组来操作model

License: MIT License

PHP 100.00%

laravel-repository's Introduction

laravel-repository

Progress Latest Stable Version Total Downloads Latest Unstable Version License GitHub stars GitHub issues GitHub forks Laravel

切换中文 | Usage of Repository

Introduction

laravel-repository provides the basic repository class for laravel model The package was made to provide more More external methods, and more friendly editor prompts; layering the code, repository is responsible for external business logic processing, model is only responsible for the definition of the fields, attributes, query conditions, and return values of the data table. It does not participate in specific logical operations, and does not serve the control layer.

Relative to the direct use of model advantages:

  • Solve the problem that model does not automatically handle extra fields when adding or modifying
  • Optimize chained calls for model queries, query directly using arrays
  • Automatically process corresponding associated data queries through query conditions and query fields
  • Provides a more friendly editor prompt

Install

Installation requirements

  • PHP >= 7.0.0
  • Laravel >= 5.5.0

1.1 Install package

composer require littlebug/laravel-repository

1.2 Use the command to generate model and repository

Suppose you have users in your database, or you replace users with the table names in your database.

php artisan core:model --table=users --name=User

The command will be at:

  • Generate User file under app/Models/ file
  • Generate UserRepository file under app/Repositories/ file

1.3 Using repository in the controller

use App\Repositories\UserRepository;

class UsersController extends Controller 
{
    /**
     * @var UserRepository
     */
    private $userRepository;
    
    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository   = $userRepository;
    }
    
    public function index()
    {
        // Paging query
        $list = $this->userRepository->paginate([
            'name:like' => 'test123', 
            'status:in' => [1, 2],
        ]);
        
        return view('users.index');
    }
    
    public function create()
    {
        list($ok, $msg, $user) = $this->userRepository->create(request()->all());
        // You are right logic
    }
    
    public function update()
    {
        list($ok, $msg, $row) = $this->userRepository->update(request()->input('id'), request()->all());
        // You are right logic
    }
    
    public function delete()
    {
        list($ok, $msg, $row) = $this->userRepository->delete(request()->input('id'));
        // You are right logic
    }
}

1.3.1 About paging query data

member message 的数据

Please check more about repository

Please check more about repository

More code generation commands

Commands support specifying database connections such as --table=dev.users

  1. core:model generates model class files and repository class files by querying database table information.

    php artisan core:model --table=users --name=User
  2. core:repository generates the repository class file

    php artisan core:repository --model=User --name=UserRepository
  3. core:request generates request verification class file by querying database table information

    php artisan core:request --table=users --path=Users

Command Parameter Details

commands of generate code

thanks for jinxing.liu and seven 💐🌹

if my repository is helpful to you, give me a star to encourage me~ ✨, I will continue to maintain this project.

laravel-repository's People

Contributors

heothuisjsb avatar mylovegy avatar wanchaochao avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.