Skip to content

Commit 316eb2f

Browse files
committed
Support Laravel 8.x
1 parent 74b37fa commit 316eb2f

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

README.md

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ $ composer require kitar/laravel-dynamodb
7272

7373
### Laravel
7474

75-
> We only support Laravel 6+ (6.x, 7.x).
75+
> We only support Laravel 6+.
7676
7777
Add dynamodb configs to config/database.php:
7878

@@ -172,11 +172,11 @@ class User extends Model implements AuthenticatableContract
172172
use Authenticatable;
173173

174174
protected $table = 'User';
175-
protected $primaryKey = 'id';
175+
protected $primaryKey = 'email';
176176
protected $sortKey = 'type';
177177
protected $sortKeyDefault = 'profile';
178178
protected $fillable = [
179-
'id', 'type', 'name'
179+
'name', 'email', 'password', 'type',
180180
];
181181
}
182182
```
@@ -226,7 +226,7 @@ User::find('[email protected]'); // Partition key. sortKeyDefault will be used for Sor
226226

227227
```php
228228
$user = new User([
229-
'id' => '[email protected]',
229+
'email' => '[email protected]',
230230
'type' => 'profile' // Sort key. If we don't specify this, sortKeyDefault will be used.
231231
]);
232232

@@ -268,7 +268,7 @@ Please refer to [Query Builder](#query-builder) for the details.
268268

269269
## Authentication with model
270270

271-
We can create a Custom User Provider to authenticate with DynamoDB. For the detail, please refer to [Laravel's official document](https://laravel.com/docs/6.x/authentication#adding-custom-user-providers).
271+
We can create a Custom User Provider to authenticate with DynamoDB. For the detail, please refer to [Laravel's official document](https://laravel.com/docs/8.x/authentication#adding-custom-user-providers).
272272

273273
To use authentication with the model, the model should implement `Illuminate\Contracts\Auth\Authenticatable` contract. In this section, we'll use the example `User` model above.
274274

@@ -296,29 +296,6 @@ public function boot()
296296
}
297297
```
298298

299-
### Modify LoginController
300-
301-
The default authentication uses the `email` and `password` column to validate, which is not the primary key of the table. However, DynamoDB needs to use the primary key to identify, so we need to tweak the LoginController a bit.
302-
303-
```php
304-
namespace App\Http\Controllers\Auth;
305-
306-
class LoginController extends Controller
307-
{
308-
...
309-
310-
/**
311-
* Get the login username to be used by the controller.
312-
*
313-
* @return string
314-
*/
315-
public function username()
316-
{
317-
return 'your-primary-key';
318-
}
319-
}
320-
```
321-
322299
### Change auth config
323300

324301
Then specify driver and model name for authentication in `config/auth.php`.
@@ -343,6 +320,45 @@ Then specify driver and model name for authentication in `config/auth.php`.
343320

344321
`api_token_name` and `api_token_index` are optional, but we need them if we use api token authentication.
345322

323+
### Registration Controller
324+
325+
You might need to modify the registration controller. For example, if we use Laravel Breeze, the modification looks like below.
326+
327+
```php
328+
class RegisteredUserController extends Controller
329+
{
330+
...
331+
332+
public function store(Request $request)
333+
{
334+
$request->validate([
335+
'name' => 'required|string|max:255',
336+
'email' => ['required', 'string', 'email', 'max:255', function ($attribute, $value, $fail) {
337+
if (User::find($value)) {
338+
$fail('The '.$attribute.' has already been taken.');
339+
}
340+
}],
341+
'password' => 'required|string|confirmed|min:8',
342+
]);
343+
344+
$user = new User([
345+
'name' => $request->name,
346+
'email' => $request->email,
347+
'password' => Hash::make($request->password),
348+
]);
349+
$user->save();
350+
351+
Auth::login($user);
352+
353+
event(new Registered($user));
354+
355+
return redirect(RouteServiceProvider::HOME);
356+
}
357+
}
358+
```
359+
360+
There are two modifications. The first one is adding the closure validator for `email` instead of `unique` validator. The second one is using the `save()` method to create user instead of the `create()` method.
361+
346362
## Query Builder
347363

348364
We can use Query Builder without model.

composer.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@
2121
},
2222
"require": {
2323
"php": "^7.2",
24-
"illuminate/support": "^6.0|^7.0",
25-
"illuminate/container": "^6.0|^7.0",
26-
"illuminate/database": "^6.0|^7.0",
27-
"illuminate/hashing": "^6.0|^7.0",
24+
"illuminate/support": "^6.0|^7.0|^8.0",
25+
"illuminate/container": "^6.0|^7.0|^8.0",
26+
"illuminate/database": "^6.0|^7.0|^8.0",
27+
"illuminate/hashing": "^6.0|^7.0|^8.0",
2828
"aws/aws-sdk-php": "^3.0"
2929
},
3030
"require-dev": {
31-
"illuminate/auth": "^6.0|^7.0",
31+
"illuminate/auth": "^6.0|^7.0|^8.0",
3232
"symfony/var-dumper": "^5.0",
3333
"vlucas/phpdotenv": "^4.1",
3434
"mockery/mockery": "^1.3",

0 commit comments

Comments
 (0)