PHP code example of littlebug / laravel-repository

1. Go to this page and download the library: Download littlebug/laravel-repository library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

littlebug / laravel-repository example snippets




use Illuminate\Routing\Controller;
use Littlebug\Repository\Tests\Stubs\UserRepository;

class UsersController extends Controller 
{
    /**
     * @var UserRepository
     */
    private $userRepository;
    
    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }
    
    public function index()
    {
        // Paging queries, returning paging objects
        $paginate = $this->userRepository->paginate([
            'name:like' => 'test', 
            'status'    => [1, 2], // Automatically converts to an in query,

            // Add complex queries and generate SQL: ("users"."status" = ? or "users"."age" >= ? or ("users"."status" = ? and "users"."age" != ?))
            // More instructions: https://wanchaochao.github.io/laravel-repository/?page=repository#5.3-%E9%A2%84%E5%AE%9A%E4%B9%89%E5%AD%97%E6%AE%B5%E6%9F%A5%E8%AF%A2
            'or' => [
                'status'  => 1,
                'age:gte' => 26,
                'and'     => [
                    'status'  => 1,
                    'age:neq' => 24,
                ],
            ],
        ], [
            'user_id',
            'username',
            
            // Statistical associated data; withCount  
            'posts_count',      

            // Query the field information for the association table if the model defines the association relationship
            'ext' => [
                'user_id',
                'ext_avatar', 
            ],
        ]);
        
        return view('users.index', compact('paginate'));
    }
    
    public function create()
    {
        // Add data and return an array
        $user = $this->userRepository->create(request()->all());
        dump($user);
    }
    
    public function update()
    {
        // Modify the data and return the number of modified rows
        $row = $this->userRepository->update(request()->input('id'), request()->all());
        dump($row);
    }
    
    public function delete()
    {
        // Deletes data and returns the number of rows deleted
        $row = $this->userRepository->delete(request()->input('id'));
        dump($row);
    }
}


use Littlebug\Repository\Tests\Stubs\UserRepository;

$paginate = UserRepository::instance()->paginate(['status' => 1]);

// Query a piece of data and return an array
$user = UserRepository::instance()->find(['status' => 1, 'id:gt' => 2]);