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';
}
}
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();
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();
});
}