Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/goldspecdigital/laravel-eloquent-uuid

A simple drop-in solution for providing UUID support for the IDs of your Eloquent models.
https://github.com/goldspecdigital/laravel-eloquent-uuid

eloquent laravel open-source uuid

Last synced: 1 day ago
JSON representation

A simple drop-in solution for providing UUID support for the IDs of your Eloquent models.

Awesome Lists containing this project

README

        


Eloquent UUID


GitHub stars


GitHub tag (latest SemVer)
Build status
Packagist
PHP from Packagist
Packagist

## Introduction

A simple drop-in solution for providing UUID support for the IDs of your
Eloquent models.

Both `v1` and `v4` IDs are supported out of the box, however should you need
`v3` or `v5` support, you can easily add this in.

## Installing

Reference the table below for the correct version to use in conjunction with the
version of Laravel you have installed:

| Laravel | This package |
|----------|--------------|
| `v5.8.*` | `v1.*` |
| `v6.*` | `v6.*` |
| `v7.*` | `v7.*` |
| `v8.*` | `v8.*` |
| `v9.*` | `v9.*` |
| `v10.*` | `v10.*` |

You can install the package via composer:

```bash
composer require goldspecdigital/laravel-eloquent-uuid:^10.0
```

## Usage

There are two ways to use this package:
1. By extending the provided model classes (preferred and simplest method).
2. By using the provided model trait (allows for extending another model class).

### Extending model

When creating a Eloquent model, instead of extending the standard Laravel model
class, extend from the model class provided by this package:

```php
id; // abb034ae-fcdc-4200-8094-582b60a4281f

// UUID explicity provided.
$model = Model::create(['id' => '04d7f995-ef33-4870-a214-4e21c51ff76e']);
echo $model->id; // 04d7f995-ef33-4870-a214-4e21c51ff76e
```

### Specifying UUID versions

By default, `v4` UUIDs will be used for your models. However, you can also
specify `v1` UUIDs to be used by setting the following property/method on your
model:

#### When extending the class

```php
toString();
}
}
```

### Creating models

In addition of the `make:model` artisan command, you will now have access to
`uuid:make:model` which has all the functionality of the standard `make:model`
command (with exception of not being able to create a pivot model):

```bash
php artisan uuid:make:model Models/Post --all
```

### Database migrations

The default primary ID column used in migrations will not work with UUID primary
keys, as the default column type is an unsigned integer. UUIDs are 36 character
strings so we must specify this in our migrations:

```php
uuid('id')->primary();
});
}
}

class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('posts', function (Blueprint $table): void {
// Primary key.
$table->uuid('id')->primary();

// Foreign key.
$table->uuid('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
}
}
```

## Running the tests

To run the test suite you can use the following command:

```bash
composer test
```

## Contributing

Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of
conduct, and the process for submitting pull requests to us.

## Versioning

We use [SemVer](http://semver.org/) for versioning. For the versions available,
see the [tags on this repository](https://github.com/goldspecdigital/laravel-eloquent-uuid/tags).

## Authors

* [GoldSpec Digital](https://github.com/goldspecdigital)

See also the list of [contributors](https://github.com/goldspecdigital/laravel-eloquent-uuid/contributors)
who participated in this project.

## License

This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md)
file for details.