How to use UUID as Primary key instead of auto-increment IDs in Laravel application?

Sdílet
Vložit
  • čas přidán 31. 07. 2024
  • What is UUID? UUIDs stands for Universally Unique Identifiers, used to uniquely identify the table records. In this, you will learn how to replace Primary key auto-increment IDs with UUIDs. And you will also learn the advantages and disadvantages of using UUIDs in your database.
    #Laravel #UUIDs
    ⌚ Timestamps:
    00:00 Introduction
    00:59 Create Fresh Laravel APP
    02:13 Create Post Model & Migration
    03:39 Remove auto-increment IDs
    05:39 Add UUID as Primary key
    07:41 Auto-generate UUID when creating a post
    11:57 Advantages and disadvantages of using UUIDs
    ▶ Laravel Tips and Tricks
    • Laravel Tips and Tricks
    ▶ Laravel Model Lifecycle Events and How to use Observers in Laravel?
    • Laravel Model Lifecycl...
    ▶ #1: SPA Authentication using Laravel Sanctum
    • #1: SPA Authentication...
    ▶ #2: API Token Authentication using Laravel Sanctum
    • #2: API Token Authenti...
    ▶ Laravel Eloquent Relationships
    • Laravel Eloquent Relat...
    ▶ Impersonating Users in Laravel And Example of TDD using PHPUnit
    • Impersonating Users in...
    ▶ Laravel Macros - How to create a Macro? How to create a class-based macro using mixin?
    • Laravel Macros - How t...
    ▶ Laravel Packages
    • Laravel Packages
    ▶ Laravel Backup Tutorial
    • Laravel Backup Tutorial
    ▶ Laravel Fortify Tutorial
    • Laravel Fortify Tutorial
    ▶ Laravel Passport Tutorials
    • Laravel Passport Tutor...
    ▶ Setup Web Development Environment Ubuntu
    • Setup Web Development ...
    Support my work:
    1. On BuyMeACoffee: www.buymeacoffee.com/qirolab
    2. On Patreon: / qirolab
    𝐃𝐢𝐠𝐢𝐭𝐚𝐥𝐎𝐜𝐞𝐚𝐧 𝐑𝐞𝐟𝐞𝐫𝐫𝐚𝐥
    m.do.co/c/e740238537d0
    Also, follow us on:
    𝐅𝐚𝐜𝐞𝐛𝐨𝐨𝐤: qirolab
    𝐓𝐰𝐢𝐭𝐭𝐞𝐫: / qirolab
  • Věda a technologie

Komentáře • 41

  • @QiroLab
    @QiroLab  Před 3 lety +1

    ⌚ Timestamps:
    00:00 Introduction
    00:59 Create Fresh Laravel APP
    02:13 Create Post Model & Migration
    03:39 Remove auto-increment IDs
    05:39 Add UUID as Primary key
    07:41 Auto-generate UUID when creating a post
    11:57 Advantages and disadvantages of using UUIDs

  • @shofada
    @shofada Před 3 lety

    Thanks for taking the time to do put the video together. I think you should add what do to if you have a live application and would want to change or "convert" the id field to a UUID field.

  • @hakan6379
    @hakan6379 Před 2 lety +1

    Thank you for video. Especially HasUuid trait trick was very helpful :) But if we delete the id field (which is auto incrementing), rows in the table will be not sorted because primary key (uuid) is a char field. To avoid that I recommend you guys to keep id field as it is and add uuid field as additional (instead of removing id field) and give it unique attribute. Then in your model, only use the HasUuid trait and add following part of code for route model binding and remove other declarations of variables in this video (such as $primaryKey);
    public function getRouteKeyName()
    {
    return 'uuid';
    }
    In this way you gonna have id and uuid fields at the same time. id still will be primary key and auto incrementing, but because of route model binding feature, you will be able to use uuid's in links.

    • @QiroLab
      @QiroLab  Před 2 lety +1

      I appreciate this, Good info. Thanks for sharing your thoughts.

  • @Pol_Otaku
    @Pol_Otaku Před 2 lety +1

    Best video of uuid !! thanks!!

  • @learnerarif7001
    @learnerarif7001 Před 2 lety

    Thank you so much. You explained very easily.

  • @darwinrcaballero9893
    @darwinrcaballero9893 Před 3 lety

    Muchas gracias amigo! Buena explicación

  • @saniamaria2303
    @saniamaria2303 Před 2 lety

    thanks so much sir

  • @David-qr2dk
    @David-qr2dk Před 2 lety +1

    This is an old video, but as a complementary, instead of using a cycle listener to generate the UUID at Post creation, we could just do it from the native DB function from the migration itself: $table->uuid('uuid')->index()->default(DB::raw('(UUID())'));
    As well as making this column as an index.
    Thanks.

    • @hassamulhaq7762
      @hassamulhaq7762 Před rokem

      I didn't know about it thanks for sharing 1 more change which is casting because how you can get the id of newly created record like $product->id solution is casting. and what about your thoughts if we drop "primary id auto increment"? according to my experiment never drop primary id, instead make 2nd column called uuid or external_id of uuid dattype char(36). I know its not because of db storage but think about if any primary_id delete suppose 5, then new record id will be 6 but in case of uuid you can use the uuid of delete row because uuid is not set primary. Even if we set uuid column as primary with auto_increment off then we can face same issue.

  • @adityakadam2256
    @adityakadam2256 Před 3 lety

    amazing videos. Thanks for sharing it :)

  • @akmalfauzansuranta7729

    savior. Tahnk you

    • @QiroLab
      @QiroLab  Před 6 měsíci

      Glad you like it!

  • @mahmoudmuhammed9585
    @mahmoudmuhammed9585 Před 3 lety

    Thanks 😊

  • @senthur251
    @senthur251 Před 3 lety

    Amazing Video! Very interesting topic. Keep going!
    Can I know what is your terminal and Vs theme?

  • @RANJEETKUMAR-wz4dg
    @RANJEETKUMAR-wz4dg Před 2 lety

    Thanks

  • @seafarindoapps
    @seafarindoapps Před rokem

    i do have table user and profile, does both of them need uuid or only profile ?

  • @mmejazhanif
    @mmejazhanif Před 3 lety

    very nice

  • @michaelgrunewalder7893

    Nice and easy to follow video, thanks - do you have a video how you configured your visual studio code for laravel development?

    • @QiroLab
      @QiroLab  Před 3 lety

      Not yet! I will create video on this as well.

  • @joaorodolphovitorcabral4197

    thank you

  • @YourHIT_
    @YourHIT_ Před rokem

    love you 3000

  • @pacykarz2009
    @pacykarz2009 Před 3 lety

    Instead of a trait, I prefer to create a parent model with $incrementing, $primaryKey, and $keyType declarations and a static boot function, that assigns uuid to the primary key (Str::uuid()->toString()). After that, it is enough to inherit this parent model :)

    • @QiroLab
      @QiroLab  Před 3 lety

      That is also a good solution.

  • @mouradouddah5934
    @mouradouddah5934 Před 3 lety

    wawwwwwwwwwwww thanks

  • @letsgozayboo
    @letsgozayboo Před 3 lety

    Need help!!!! take 1hour to import 50k records laravel matwebsite package what to do??THanks

    • @QiroLab
      @QiroLab  Před 3 lety +1

      You should use Laravel queue for that. I will create video on this as well.

  • @TheMeenaDhiraj
    @TheMeenaDhiraj Před 2 lety

    Hiii sir

  • @TheMeenaDhiraj
    @TheMeenaDhiraj Před 2 lety

    I ask a question about
    Crack APK

  • @hasusuf
    @hasusuf Před 2 lety

    UUID should be string and not class. Replace `Str::uuid()` to `Str::uuid()->toString()`

  • @Shez-dc3fn
    @Shez-dc3fn Před 3 lety

    for the love of God - use shortcuts so u dont have to type full php artisan migrate etc instead a:m or something else.. we dont need to see you type the whole thing - applies to everyone not just this video