开源软件名称(OpenSource Name):michaeldyrynda/laravel-model-uuid开源软件地址(OpenSource Url):https://github.com/michaeldyrynda/laravel-model-uuid开源编程语言(OpenSource Language):PHP 100.0%开源软件介绍(OpenSource Introduction):Laravel Model UUIDsIntroductionI find myself using UUID across multiple projects of late, and packaged up this functionality rather than copying and pasting it from project to project. Note: this package explicitly does not disable auto-incrementing on your Eloquent models. In terms of database indexing, it is generally more efficient to use auto-incrementing integers for your internal querying. Indexing your For more information, check out this post on storing and working with UUID in an optimised manner. Take a look at laravel-efficient-uuid if you want to make it easy to generate migrations that efficiently store UUID in your database. If you require compatibility with As of version 6.2.0, this package supports only UUID versions 1 ( Code SamplesIn order to use this package, you simply need to import and use the trait within your Eloquent models. <?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Dyrynda\Database\Support\GeneratesUuid;
class Post extends Model
{
use GeneratesUuid;
} It is assumed that you already have a field named class Post extends Model
{
public function uuidColumn(): string
{
return 'custom_column';
}
} You can have multiple UUID columns in each table by specifying an array in the class Post extends Model
{
public function uuidColumns(): array
{
return ['uuid', 'custom_column'];
}
} By default, this package will use UUID version 4 values, however, you are welcome to use <?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Dyrynda\Database\Support\GeneratesUuid;
class Post extends Model
{
use GeneratesUuid;
protected $uuidVersion = 'uuid5';
} Whilst not recommended, if you do choose to use a UUID as your primary model key ( <?php
namespace App;
use Dyrynda\Database\Support\GeneratesUuid;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use GeneratesUuid;
public $incrementing = false;
protected $keyType = 'string';
} This trait also provides a query scope which will allow you to easily find your records based on their UUID, and respects any custom field name you choose. // Find a specific post with the default (uuid) column name
$post = Post::whereUuid($uuid)->first();
// Find multiple posts with the default (uuid) column name
$post = Post::whereUuid([$first, $second])->get();
// Find a specific post with a custom column name
$post = Post::whereUuid($uuid, 'custom_column')->first();
// Find multiple posts with a custom column name
$post = Post::whereUuid([$first, $second], 'custom_column')->get(); If you use the suggested laravel-efficient-uuid package, you will need to add a cast to your model in order to correctly set and retrieve your UUID values. This will ensure your UUIDs are written to your (MySQL) database as binary and presented as strings. <?php
namespace App;
use Dyrynda\Database\Casts\EfficientUuid;
use Dyrynda\Database\Support\GeneratesUuid;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use GeneratesUuid;
protected $casts = [
'uuid' => EfficientUuid::class,
];
} Route model bindingFrom 6.5.0, should you wish to leverage implicit route model binding on your <?php
namespace App;
use Dyrynda\Database\Support\BindsOnUuid;
use Dyrynda\Database\Support\GeneratesUuid;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use BindsOnUuid, GeneratesUuid;
} Should you require additional control over the binding, or are using < 6.5.0 of this package, you may override the public function getRouteKeyName(): string
{
return 'uuid';
} If you are using the laravel-efficient-uuid package, implicit route model binding won't work out of the box. Laravel will execute the query using the string representation of the // app/Providers/RouteServiceProvider.php
public function boot()
{
Route::bind('post', function ($post) {
return \App\Post::whereUuid($post)->first();
});
} InstallationThis package is installed via Composer. To install, run the following command. composer require dyrynda/laravel-model-uuid SupportIf you are having general issues with this package, feel free to contact me on Twitter. If you believe you have found an issue, please report it using the GitHub issue tracker, or better yet, fork the repository and submit a pull request. If you're using this package, I'd love to hear your thoughts. Thanks! TreewareYou're free to use this package, but if it makes it to your production environment you are required to buy the world a tree. It’s now common knowledge that one of the best tools to tackle the climate crisis and keep our temperatures from rising above 1.5C is to plant trees. If you support this package and contribute to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats. You can buy trees here Read more about Treeware at treeware.earth |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论