Search
 
SCRIPT & CODE EXAMPLE
 

PHP

uuid in laravel

use IlluminateSupportStr;

$uuid = Str::uuid()->toString();
Comment

uuid table laravel

$table->uuid('id');
Comment

uuid laravel

use IlluminateSupportStr;

$isUuid = Str::isUuid('a0a2a2d2-0b87-4a18-83f2-2529882be2de');

// true

$isUuid = Str::isUuid('laravel');

// false
Comment

UUIDs LARAVEL

//above in controller
use IlluminateSupportStr;

$uuid = (string) Str::uuid()
Comment

how to use uuid in laravel model

1) Implement Uuid trait.

// app/Traits/Uuid.php 
<?php

namespace AppTraits;

use Exception;
use IlluminateDatabaseEloquentModel;
use IlluminateSupportStr;

trait Uuid
{
    protected static function boot()
    {
        parent::boot();

        static::creating(function (Model $model) {
            try {
                $model->id = Str::uuid()->toString();
            } catch (Exception $e) {
                abort(500, $e->getMessage());
            }
        });
    }
}

2) Go to the model class and set $incrementing to false;

// app/Models/*.php
class Whatever implements Model {
	public $incrementing = false;
  	...
}

3) Go to the migration class and set uuid as primary key;
// database/migrations/*.php
class CreateWhateverTable extends Migration {
	public function up()
  	{
      Schema::create("whatever", function (Blueprint $table) {
          // You can change "id" to "uuid" or whatever...	  
          $table->uuid("id")->primary();
          // another column
      });
  	}
}
Comment

PREVIOUS NEXT
Code Example
Php :: unique validation laravel 
Php :: laravel sanctum 
Php :: PHP temporary files 
Php :: Get data from array (from an API) in Laravel 
Php :: laravel custom abort message 
Php :: add character after x characters in php 
Php :: php into javascript 
Php :: get element by index array php 
Php :: how to check if a user is logged in in a non middleware controller in laravel 
Php :: how check the time of operation in laravel 
Php :: mktime syntax php 
Php :: license_verify 
Php :: test php code online free 
Php :: wp php blog info image 
Php :: PHP strtok — Tokenize string 
Php :: call satic blco in magento 2 
Php :: php remove non printable characters 
Php :: php fake stripe client 
Php :: empty func php 
Php :: change wordpress viewport 
Php :: php mysql update all rows in table random values 
Php :: make php website https 
Php :: log magenot 1 
Php :: eloquent multiple orwhere 
Php :: laravel withcount change name 
Php :: laravel database engine innodb 
Php :: php rand between 0 and 1 
Php :: drupal show php errors 
Php :: php 7 The each() function is deprecated. 
Php :: remove php 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =