Giter Club home page Giter Club logo

fastd-eloquent's Introduction

fastD Eloquent

使用 Eloquent 替换 fastD 自带的 Medoo

安装

$ composer require -vvv zqhong/fastd-eloquent

配置

修改配置文件 config/app.conf,添加 EloquentServiceProvider,如下:

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

修改配置文件 .env.yml,添加如下信息:

database:
    default:
        adapter: db_adapter
        name: db_name
        host: db_host
        user: db_username
        pass: db_password
        charset: db_charset
        port: db_port
        prefix: db_prefix

其中,driver 的可选值为:mysqlpgsqlsqlitesqlsrv

直接使用

<?php
<?php

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();

配置 Model 使用

<?php

namespace Model;

use Illuminate\Database\Eloquent\Model;

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

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

分页

分页的简单使用

<?php
$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);
//});

其他资源

如果你对在其他框架中使用 Eloquent 感兴趣,请参考 Slim 的这篇文章 - Using Eloquent with Slim

如果你对 Eloquent 不熟悉,请阅读下面的资料。

fastd-eloquent's People

Contributors

marlonfan avatar zqhong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

fastd-eloquent's Issues

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.