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

Поделиться
HTML-код
  • Опубликовано: 8 сен 2024

Комментарии • 41

  • @QiroLab
    @QiroLab  3 года назад +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

  • @hakan6379
    @hakan6379 2 года назад +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  2 года назад +1

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

  • @shofada
    @shofada 3 года назад

    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.

  • @saniamaria2303
    @saniamaria2303 2 года назад

    thanks so much sir

  • @akmalfauzansuranta7729
    @akmalfauzansuranta7729 2 года назад

    savior. Tahnk you

    • @QiroLab
      @QiroLab  7 месяцев назад

      Glad you like it!

  • @David-qr2dk
    @David-qr2dk 2 года назад +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 Год назад

      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.

  • @Pol_Otaku
    @Pol_Otaku 3 года назад +1

    Best video of uuid !! thanks!!

  • @learnerarif7001
    @learnerarif7001 2 года назад

    Thank you so much. You explained very easily.

    • @QiroLab
      @QiroLab  2 года назад

      You are welcome!

  • @joaorodolphovitorcabral4197
    @joaorodolphovitorcabral4197 Год назад

    thank you

  • @darwinrcaballero9893
    @darwinrcaballero9893 3 года назад

    Muchas gracias amigo! Buena explicación

    • @QiroLab
      @QiroLab  3 года назад

      Thank you 😊

  • @senthur251
    @senthur251 3 года назад

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

  • @RANJEETKUMAR-wz4dg
    @RANJEETKUMAR-wz4dg 2 года назад

    Thanks

  • @mmejazhanif
    @mmejazhanif 3 года назад

    very nice

  • @seafarindoapps
    @seafarindoapps Год назад

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

  • @adityakadam2256
    @adityakadam2256 3 года назад

    amazing videos. Thanks for sharing it :)

    • @QiroLab
      @QiroLab  3 года назад

      Glad you like them!

  • @mahmoudmuhammed9585
    @mahmoudmuhammed9585 3 года назад

    Thanks 😊

    • @QiroLab
      @QiroLab  3 года назад

      You're welcome!

  • @michaelgrunewalder7893
    @michaelgrunewalder7893 3 года назад

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

    • @QiroLab
      @QiroLab  3 года назад

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

  • @pacykarz2009
    @pacykarz2009 3 года назад

    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  3 года назад

      That is also a good solution.

  • @YourHIT_
    @YourHIT_ 2 года назад

    love you 3000

    • @QiroLab
      @QiroLab  Год назад

      Thank you so much

  • @letsgozayboo
    @letsgozayboo 3 года назад

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

    • @QiroLab
      @QiroLab  3 года назад +1

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

  • @mouradouddah5934
    @mouradouddah5934 3 года назад

    wawwwwwwwwwwww thanks

    • @QiroLab
      @QiroLab  3 года назад

      Glad you like them!

  • @TheMeenaDhiraj
    @TheMeenaDhiraj 3 года назад

    Hiii sir

  • @TheMeenaDhiraj
    @TheMeenaDhiraj 3 года назад

    I ask a question about
    Crack APK

  • @hasusuf
    @hasusuf 2 года назад

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

  • @Shez-dc3fn
    @Shez-dc3fn 3 года назад

    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