1. Go to this page and download the library: Download dyrynda/laravel-model-uuid library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dyrynda / laravel-model-uuid example snippets
namespace App;
use Illuminate\Database\Eloquent\Model;
use Dyrynda\Database\Support\GeneratesUuid;
class Post extends Model
{
use GeneratesUuid;
}
class Post extends Model
{
public function uuidColumn(): string
{
return 'custom_column';
}
}
class Post extends Model
{
public function uuidColumns(): array
{
return ['uuid', 'custom_column'];
}
}
namespace App;
use Illuminate\Database\Eloquent\Model;
use Dyrynda\Database\Support\GeneratesUuid;
class Post extends Model
{
use GeneratesUuid;
public function uuidVersion(): ?string
{
return 'uuid5';
}
}
return [
/**
* The default uuid version used to generate UUID value.
*/
'uuid_version' => 'uuid4',
];
namespace App;
use Dyrynda\Database\Support\GeneratesUuid;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use GeneratesUuid;
public $incrementing = false;
protected $keyType = 'string';
}
// 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();
// Exclude a specific post using the default (uuid) column name
$post = Post::whereNotUuid($uuid)->first();
// Exclude multiple posts using the default (uuid) column name
$post = Post::whereNotUuid([$first, $second])->get();
// Exclude a specific post using a custom column name
$post = Post::whereNotUuid($uuid, 'custom_column')->first();
// Exclude multiple posts using a custom column name
$post = Post::whereNotUuid([$first, $second], 'custom_column')->get();
namespace App;
use Dyrynda\Database\Support\Casts\EfficientUuid;
use Dyrynda\Database\Support\GeneratesUuid;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use GeneratesUuid;
protected $casts = [
'uuid' => EfficientUuid::class,
];
}
namespace App;
use Dyrynda\Database\Support\BindsOnUuid;
use Dyrynda\Database\Support\GeneratesUuid;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use BindsOnUuid, GeneratesUuid;
}
public function getRouteKeyName(): string
{
return 'uuid';
}
// app/Providers/RouteServiceProvider.php
public function boot()
{
Route::bind('post', function ($post) {
return \App\Post::whereUuid($post)->first();
});
}
namespace App;
use Dyrynda\Database\Support\Casts\EfficientUuid;
use Dyrynda\Database\Support\GeneratesUuid;
use Dyrynda\Database\Support\UsesBinaryUuidBuilder;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use GeneratesUuid, UsesBinaryUuidBuilder;
public $incrementing = false;
protected $keyType = 'string';
public function uuidColumn(): string
{
return 'id';
}
protected $casts = [
'id' => EfficientUuid::class,
];
public function posts()
{
return $this->hasMany(Post::class);
}
}
namespace App;
use Dyrynda\Database\Support\Casts\EfficientUuid;
use Dyrynda\Database\Support\GeneratesUuid;
use Dyrynda\Database\Support\UsesBinaryUuidBuilder;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use GeneratesUuid, UsesBinaryUuidBuilder;
public function uuidColumns(): array
{
return ['user_id'];
}
protected $casts = [
'user_id' => EfficientUuid::class,
];
public function user()
{
return $this->belongsTo(User::class);
}
}
// Eager loading
$posts = Post::with('user')->get();
// Lazy loading
$user = $post->user;
$posts = $user->posts;
// Standard where clauses on binary UUID columns
$user = User::where('id', $uuid)->first();
// Relationship existence queries
$usersWithPosts = User::whereHas('posts')->get();