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 :: if condition inside echo in php 
Php :: laravel tinker generate password 
Php :: add blade in blade laravel 
Php :: php jquery plus 1 day 
Php :: linux delete php sessions 
Php :: laravel database get all 
Php :: how to check mobile or desktop in php 
Php :: how to get local current time in laravel 
Php :: Delete an array in multidimensional array php 
Php :: convert image to base64 in laravel 
Php :: find curren monday in laravel carbon 
Php :: php file put content 
Php :: convert byte to megabyte php 
Php :: php isset multiple 
Php :: use latest with first in laravel eloquent 
Php :: how to use postgresql with laravel 
Php :: wordpress programmatically logout 
Php :: php replace all spaces with dashes 
Php :: laravel sail publish 
Php :: wordpress get attachment url by size 
Php :: LARAVEL CREAT NEW TEST 
Php :: reset password multipple database laravel 
Php :: laravel get timezone from ip address 
Php :: total days between two dates carbon 
Php :: carbon subtract two dates 
Php :: php switch statement 
Php :: how remove empty value in array php 
Php :: how to retrieve data from database using select option in laravel 
Php :: mysql secure 
Php :: How to use my constants in Larvel 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =