My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more
Generate UUID on your laravel model

Generate UUID on your laravel model

Afowowe Olumide's photo
Afowowe Olumide
·Apr 17, 2020

If you don't have laravel installed on your system it's time you check laravel site for documentation on how to install laravel installing laravel

We start by creating new laravel project
Opening your terminal and writing this command

laravel new uuidsample

After successfully creating our project open your folder in visual studio code or any code editor you have.
To generate uuid we need a table where we will store data with unique id; to achieve this we will need to make migration that generate out table [Books table] Go to your terminal in your visual studio code and input this command

php artisan make:migration create_books_table

Migration has been created go to your database folder under migration there php file with name
2020_02_12_155849_create_books_table.php file name depending on the date the migration was created followed by those suffixes

Modify the php file to look like the code below take note of how the uuid is written

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateBooksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('author');
            $table->uuid('uuid')->uinique();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('books');
    }
}

Now we migrate our table by running the command below

php artisan migrate

this will create a new database table

We create a model for the table created

php artisan make:model Books

The model will be found under app folder app/Books

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class Bools extends Model
{
    public $table = 'books';
    protected $fillable = [ 'name', 'author', 'uuid'];

}

Now we generate a controller for our model

php artisan make:controller BooksController

Books controller is located in app/http where the major work will be done
Modify book controller to this

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Str;
use App\Books;
use Illuminate\Http\Request;

class BooksController extends Controller
{
    public function Createbook(Request $request){
        $input = $request->all();
        $input['uuid'] = (string) Str::uuid();        
        $book = Books::create($input);
        return response()->json([
            "status" => "success",
            "data" => $book,
            "message" => "."
            ], 200
        );
    }
}

We used the laravel str helper function to generate a unique uuid for every books created