Typical (Junior?) Mistake: Hardcoded Values

Поділитися
Вставка
  • Опубліковано 14 жов 2024

КОМЕНТАРІ • 43

  • @fawad0301
    @fawad0301 2 місяці тому +4

    Thank you, very much, your every video increase my knowledge, Keep it up. God bless You.

  • @hardcoreteunes
    @hardcoreteunes 2 місяці тому +14

    very nice, i'm demoted to junior

    • @He4vyD
      @He4vyD 2 місяці тому +3

      I believe “junior” and “senior” reflect on the experience and quality of the code, not the amount of years you’ve been programming.

  • @emmanuelingelaere8500
    @emmanuelingelaere8500 2 місяці тому +4

    Thanks for the intresting subject.
    To prevent different id's in different environments after running the seeders, I always include the id value in the seeders for picklists like so:
    public function run(): void
    {
    $statusOptions = [
    ['id' => 1, 'name' => 'open'],
    ['id' => 2, 'name' => 'closed'],
    ];
    foreach ($statusOptions as $statusOption) {
    ItemStatus::create($statusOption);
    }
    }
    Then I use enums or constants in my code much like demonstrated here. Also, I always use the fieldname 'name' for the description in picklists, because that's easy to remember.

    • @LaravelDaily
      @LaravelDaily  2 місяці тому

      Good approach!

    • @kakosnaki
      @kakosnaki 2 місяці тому

      In my experience (with Doctrine and EF) ORMs ignore ID value on insert if the field is declared auto increment. Is AI supposed to be toggled off during data seeding like this or is there a better way?
      $metadata = $this->entityManager->getClassMetaData($className);
      $metadata->setIdGeneratorType(\Doctrine\ORM\Mapping\ClassMetadata::GENERATOR_TYPE_NONE);

  • @AbderrahmanFodili
    @AbderrahmanFodili 2 місяці тому +1

    amazing
    instead of model constants,I started using enums for the role names and add many other methods just to encapsulate related logic. thanks to one of you videos as well

  • @obadaalzidi6452
    @obadaalzidi6452 2 місяці тому +2

    Thanks for the great video.
    In my projects I never use $user->role->name outside of the User model, I create a model method eg. hasRole(string $role).
    So in the future if we decided to let the user have multiple roles, the refactor is so easy.

  • @seifahmed1695
    @seifahmed1695 2 місяці тому +2

    Thank you for this video, I started using Enums recently and it helped me alot

  • @3Descape
    @3Descape 2 місяці тому +3

    Such hardcoded values get especially complex, when your JS frontent has to be aware of them too (show certain data if value == someConstant, the user should be able to choose on of them in a select, etc..). In a recent project I've written a blade macro to expose my php enums to js, so I don't have to maintain the key,values in both places. Benefit: I now get errors when I try to access invalid keys, so I'm at least aware of the problem and it doesn't fail silently.
    Where it made sense, I moved the logic to the backend and only exposed the final value/precomputed value as json to the frontend, to avoid using the constants in the frontend in the first place.

  • @sarjamdaschool7392
    @sarjamdaschool7392 2 місяці тому +2

    In scenarios like these, I typically add an extra column named `type` to the table. Subsequently, I define a global scope in the model to retrieve the corresponding ID of the role by searching via the `type` column.
    In the model, the scope is defined as follows:
    ```php
    public function scopeRiskOfficer($query){
    return $query->where('type', 'risk-officer')->first();
    }
    ```
    To retrieve the ID of the risk officer role, you can use:
    ```php
    Role::riskOfficer()->id;
    ``

  • @DavidHermo-uh7xe
    @DavidHermo-uh7xe 2 місяці тому +1

    What do you think about putting the ids in the config files in an array key => value(id) with a descriptive key name. In this case broker => 2, so that $user->role_id == config('roles.broker') ?

  • @jsan5709
    @jsan5709 2 місяці тому

    Thanks for this nice advice, don't seems obvious at begining, and may looks that its more boilerplate code, but in the future you will appreciate.
    Moreover I think that use a Enums for kind type of data, even prefer than db table, for more consistency in your code.
    For this particular case i'ld create a RolesEnum file, and there I'll store all roles.

  • @J87NL
    @J87NL 2 місяці тому +5

    When switching to enums, the question is if you still need the table and records in the database. Except when there are other columns besides id and name of course.

    • @DanielHaliscak
      @DanielHaliscak 2 місяці тому +1

      I would still use lookup tables, even if there is enum. To keep data consistent.
      If i hard delete a value from LU table, having a constrain would prevent it.

  • @DanielHaliscak
    @DanielHaliscak 2 місяці тому +2

    I dont agree. This is not just about role_id. But lookups in general.
    I would rather make sure that IDs are same across environments. They just have to be. ID is not changing, name could. I dont want to compare if strings are matching, but rather IDs.
    Let say you have stored procedures, db funcions, views, these don't have enums, so you have to hardcode ID. I dont want to use string comparison there.
    Running imports from excel/running import scripts from DB to DB. IDs have to be same in all environments so I could first test it in QA/staging. I dont want to amend my scripts per env.
    Having integrations between systems, like sending booking to financtial system, I really dont want to send for example Booking Type = Concert, but ID. Then external system should have same IDs as my system, not necesary same primary key, but foreign key, so they could map it.
    I dont get it, if someone has different ID per environment, that sounds like junior mistake.
    At the same time I agree not to hardcode it to 1, 2, 3, etc. in code, but rather use enums/constants, which would be just masked ID. It would make code more readable.

  • @BelgranoK
    @BelgranoK 2 місяці тому +3

    If you use Filament 3 choose the enum and I think the table roles is useless in this case.

  • @silentwave1325
    @silentwave1325 2 місяці тому

    It's not just "protected from future change in the name", it can be as simple as protected from misspelling or string cases, broker instead of Broker

  • @Mohamed.G15
    @Mohamed.G15 2 місяці тому

    Nice tutorial
    Thanks

  • @fabrizziomorenor.6495
    @fabrizziomorenor.6495 2 місяці тому

    Thanks! Good code improvements.

  • @theaungmyatmoe
    @theaungmyatmoe 2 місяці тому +1

    I use Enum for that type of issue, it would be better to use const, I think so.

  • @HammadZafar-u9r
    @HammadZafar-u9r 2 місяці тому

    How to select only required columns from db instead of loading all columns in filament tables?

  • @OrkhanShafizada
    @OrkhanShafizada 2 місяці тому

    hello. i need to help. my laravel project is working correctly in local. but when i upload my project in prod. cookies not set. i cant solve it

  • @BlueJDev
    @BlueJDev 2 місяці тому +4

    Just FYI, you seem to be getting quieter in every video I watch. Other videos I've watched on this device were fine but at max volume I was struggling to hear you.

    • @LaravelDaily
      @LaravelDaily  2 місяці тому +4

      Yeah I noticed as well that in this video I was talking more quietly, with less energy.
      I guess I need to get some sleep more. Or take a few weeks off shooting videos.

    • @BlueJDev
      @BlueJDev 2 місяці тому +1

      @@LaravelDaily you have sounded like you have a cold the last few, but not to the point where the video is a bad lesson. You "sound" fine, I just think your mic levels need adjusting or possibly the mic needs to be closer to you.

  • @amirhossein1044
    @amirhossein1044 2 місяці тому

    Hi, do you have any video about throwing ValidationException::withMessage and comparing it to back()->withErrors...?

    • @LaravelDaily
      @LaravelDaily  2 місяці тому

      Not specifically about those, no.

    • @amirhossein1044
      @amirhossein1044 2 місяці тому +1

      @@LaravelDaily I saw that in laravel breeze source and wondered why they are used, I think it's easier to throw exceptions from sub methods rather than return

  • @TrikNgonlen
    @TrikNgonlen 2 місяці тому

    AWESOME! Always love this channel...

  • @RogiervanCann
    @RogiervanCann 2 місяці тому +1

    I always use an `alias` column which can't be manually changed in front-end

  • @roberttudor_
    @roberttudor_ 2 місяці тому

    How about using Caleb Porzio's Sushi package?
    If you insist on using Eloquent and all its features, if the data in that "table" rarely changes (if ever!) you might as well go for an array "table" using Sushi.

    • @LaravelDaily
      @LaravelDaily  2 місяці тому

      I never found a real use-case of this, personally, but yes it's an option.

  •  2 місяці тому

    Thanks, good examples 👌🏽

  • @nurullahdemirel6250
    @nurullahdemirel6250 2 місяці тому +1

    I think in this situation enum is better than this also you can use enum when filter by role it is more readable and maintainable I think

  • @egwuted2398
    @egwuted2398 2 місяці тому

    Please which type of browser is that?

  • @vonsofh
    @vonsofh 2 місяці тому

    I'm using laravel-enum for sample like that

  • @badmuskaybee5111
    @badmuskaybee5111 2 місяці тому

    I prefer to do that manually