1. Go to this page and download the library: Download mawuekom/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/ */
mawuekom / laravel-model-uuid example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Mawuekom\ModelUuid\Utils\GeneratesUuid;
class Post extends Model
{
use GeneratesUuid;
}
class Post extends Model
{
/**
* The names of the column that should be used for the UUID.
*
* @return string
*/
public function uuidColumn(): string
{
return 'custom_column';
}
}
class Post extends Model
{
/**
* The names of the columns that should be used for the UUID.
*
* @return array
*/
public function uuidColumns(): array
{
return ['uuid', 'custom_column'];
}
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Mawuekom\ModelUuid\Utils\GeneratesUuid;
class Post extends Model
{
use GeneratesUuid;
protected $uuidVersion = 'uuid5';
}
// 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\Http\Controllers;
use Illuminate\Database\Eloquent\Model;
use Mawuekom\ModelUuid\Utils\ValidatesUuid;
class PostController extends Controller
{
use ValidatesUuid;
public function getPostByUuid($uuid)
{
$this ->validatesUuid('custom_column', $uuid, Post::class);
}
}