PHP code example of zqhong / fastd-eloquent

1. Go to this page and download the library: Download zqhong/fastd-eloquent 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/ */

    

zqhong / fastd-eloquent example snippets



return [
    // 省略了无关配置
    'services' => [
        \ServiceProvider\EloquentServiceProvider::class,
    ],
];




use Illuminate\Database\Capsule\Manager;

// create
Manager::connection('default')
    ->table('demo')
    ->insert([
        'content' => 'hello world',
    ]);

// read
// 参数一可省略,默认值为 default
Manager::connection('default')
    ->table('demo')
    ->where('id', 1)
    ->where('created_at', '<=', time())
    ->get([
        'id',
        'content',
    ]);

// update
Manager::connection('default')
    ->table('demo')
    ->where('id', 1)
    ->update([
        'content' => 'hello 2',
    ]);

// delete
Manager::connection('default')
    ->table('demo')
    ->where('id', 1)
    ->delete();




namespace Model;

use Illuminate\Database\Eloquent\Model;

class DemoModel extends Model
{
    public $table = 'demo';

    public static function fetchAll()
    {
        return static::query()
            ->get()
            ->toArray();
    }
}


$perPage = 10;
$columns = ['*'];
$pageName = 'page';

$paginator = YourModel::query()->paginate($perPage, $columns, $pageName);
$data = $paginator->toArray();

// 注:page 参数(当前页)会自动从 $_GET 或 $_POST 中的 page 参数自动获取,不需要单独设置。
// 参考代码
//LengthAwarePaginator::currentPageResolver(function () {
//    return (int)Arr::get(array_merge($_GET, $_POST), 'page', 1);
//});