Laravel DynamoDB Eloquent 模型和查询构建器

Laravel

Laravel DynamoDB 是一个基于 DynamoDB-based Eloquent 模型和查询构造器。 使用提供的 Dynamo 驱动程序,模型扩展了 Eloquent 基础模型:

use Kitar\Dynamodb\Model\Model;

class ProductCatalog extends Model
{
    // 需要
    protected $table = 'ProductCatalog';

    // 主键的命名 (需要)
    protected $primaryKey = 'Id';

    // 排序键值的命名 (可选)
    protected $sortKey = 'Subject';

    // 当我们使用 find 不使用排序键时,默认的排序键值。
    protected $sortKeyDefault = 'profile';

    protected $fillable = ['Id', 'Price', 'Title'];
}

以下是 readme 中的几个示例,您可以使用此程序包的查询和操作:

// 获取所有模型
$products = ProductCatalog::scan();
// 或者
$products = ProductCatalog::all();

// 分页
$products = ProductCatalog::limit(5)->scan();

// 创建一个用户
$user = User::create([
    'email' => 'foo@bar.com',
    // Sort key. If we don't specify this,
    // sortKeyDefault will be used.
    'type' => 'profile',
]);

// 实例化一个模型然后保存。
$user = new User([
    'email' => 'foo@bar.com',
    'type' => 'profile'
]);

$user->save();

// 更新一个存在的模型
$user->update([
    'name' => 'foobar'
]);

这个软件包包含了更高级的 Dynamo 用法,包括您可以在没有模型的情况下使用的查询构造器 (也可以在 Laravel 之外使用)。此外,查询构造器还支持 条件表达式。例如:

DB::table('ProductCatalog')
    ->condition('Id', 'attribute_not_exists')
    ->orCondition('Price', 'attribute_not_exists')
    ->putItem([/*...*/]);

以下是一个 过滤表达式 的实例,它可以在数据库返回结果之前使用表达式筛选结果:

$response = DB::table('Thread')
    ->filter('LastPostedBy', '=', 'User A')
    ->scan();

// orFilter()
$response = DB::table('Thread')
    ->filter('LastPostedBy', '=', 'User A')
    ->orFilter('LastPostedBy', '=', 'User B')
    ->scan();

// filterIn
$response = DB::table('Thread')
    ->filterIn('LastPostedBy', ['User A', 'User B'])
    ->scan();

// filterBetween
$response = DB::table('ProductCatalog')
    ->filterBetween('Price', [0, 100])
    ->scan();

DynamoDB 包还包含有关使用 DynamoDB 模型进行 用户身份验证 的详细说明。 你可以了解有关 DynamoDB 包的更多信息、获取完整的安装说明,并在 GitHub 上查看 源代码

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://laravel-news.com/laravel-dynamod...

译文地址:https://learnku.com/laravel/t/67073

本文为协同翻译文章,如您发现瑕疵请点击「改进」按钮提交优化建议
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!