kitar / laravel-dynamodb

A DynamoDB based Eloquent model and Query builder for Laravel.
MIT License
179 stars 27 forks source link

Call to undefined method Illuminate\Database\Query\Builder::usingModel() #19

Closed andrewboohoo closed 2 years ago

andrewboohoo commented 2 years ago

I am getting error

Call to undefined method Illuminate\Database\Query\Builder::usingModel()

when trying to use package, for example

$refunds = RefundsLog::scan();

Laravel v8.83.12

kitar commented 2 years ago

@andrewboohoo Thanks for the report! Can you share the code of RefundsLog model? (especially, the parts that use Eloquent features such as casts)

There may be some Eloquent features that this package not supported yet.

Jonnx commented 2 years ago

I am getting the same error using the following model just to try it out (dynamodb-local in docker)

 'dynamodb' => [
            'driver' => 'dynamodb',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
            'token' => env('AWS_SESSION_TOKEN', null),
            'endpoint' => env('DYNAMODB_ENDPOINT', null),
            'prefix' => 'someapp_' . config('app.env') . '_', // table prefix
        ],
<?php

namespace App\Models;

use Kitar\Dynamodb\Model\Model;

class DynamoUser extends Model
{
    protected $table = 'users';
    protected $primaryKey = 'email';
    protected $fillable = ['email', 'name', 'password'];
}
umayanrmr commented 2 years ago

I solved it by adding a protected $connection = 'dynamodb'; to my model

kitar commented 2 years ago

I finally understand! Thank you for reporting @umayanrmr @Jonnx

You have to set DB_CONNECTION=dynamodb in the .env file. If you do so, you don't need to override protected $connection. This is not in the document, so I'll add it soon.

kitar commented 2 years ago

I'll close this issue at this time. Please feel free to re-open if needed.

gvanto commented 7 months ago

I finally understand! Thank you for reporting @umayanrmr @Jonnx

You have to set DB_CONNECTION=dynamodb in the .env file. If you do so, you don't need to override protected $connection. This is not in the document, so I'll add it soon.

What if I want to use both MySQL and DynamoDB in my application?

kitar commented 7 months ago

@gvanto I believe you can manage it in the same way you would handle multiple MySQL databases. https://fideloper.com/laravel-multiple-database-connections

gvanto commented 7 months ago

Thanks @kitar, think I will just use the below in my model as it seems to work just fine:

protected $connection = 'dynamodb';