Laravel: 4 Ways to Fix "Attempt property on null" Error

แชร์
ฝัง
  • เผยแพร่เมื่อ 20 เม.ย. 2024
  • If you use a relationship in Laravel which may not exist, there are multiple ways to avoid the error on the screen.
    My course "Eloquent: Expert Level" laraveldaily.com/course/larav...
    - - - - -
    Support the channel by checking out my products:
    - My Laravel courses: laraveldaily.com/courses
    - Filament examples: filamentexamples.com
    - Livewire Kit Components: livewirekit.com
    - - - - -
    Other places to follow:
    - My weekly Laravel newsletter: us11.campaign-archive.com/hom...
    - My personal Twitter: / povilaskorop
  • แนวปฏิบัติและการใช้ชีวิต

ความคิดเห็น • 57

  • @philipjamesajagabos2519
    @philipjamesajagabos2519 27 วันที่ผ่านมา +13

    I use the double question mark " ??" because I can set a default on null but that 4th method using the model is superb. Write one and use everywhere 💡

    • @RadosawMrowiecRudobrody
      @RadosawMrowiecRudobrody 26 วันที่ผ่านมา

      Exactly the same here :) I love 4th approach 🤘🏻🍻

  • @Maniac-007
    @Maniac-007 10 วันที่ผ่านมา

    No 4 is definitely the GOATed way to handle it

  • @stephanpaquet
    @stephanpaquet 24 วันที่ผ่านมา +1

    Did not know about default relation attribute .. wow thanks

    • @macdanson4430
      @macdanson4430 24 วันที่ผ่านมา +1

      Honestly, I have also seen for the first time

  • @FririkurEllefsen
    @FririkurEllefsen 20 วันที่ผ่านมา +1

    I learned alot as always, but I must admit that I would have used forelse and then empty

  • @sulaimanmisri6411
    @sulaimanmisri6411 26 วันที่ผ่านมา +2

    I love the 4th method. It's new to me and I no need to code that null safe operator again and again.

  • @ArshAudioBooks
    @ArshAudioBooks 26 วันที่ผ่านมา +2

    today i accidentally deleted some users, and i forgot that they had some posts in db. i had this error today. and i don't remember how i fixed it. but i fixed it. i became lazy on this video, thought this would not help me. but nope.😂 you are just awesome. i will simply download your whole channel 😂

  • @muhammadsalman2182
    @muhammadsalman2182 27 วันที่ผ่านมา

    It is really helping I did not know these before.❤

  • @ggoparent
    @ggoparent 27 วันที่ผ่านมา

    Only knew the third one, has always been used it, but the 4th is amazing will try it in the future! Thank you very much

  • @VipStudio
    @VipStudio 26 วันที่ผ่านมา +2

    also you can do @ as well in front of variable like @$post->category->name you can try that.. i hope it would help.

  • @IlPandax
    @IlPandax 27 วันที่ผ่านมา

    Thanks! I didn't know the 4th. Great video! ;)

  • @DevduttaBain
    @DevduttaBain 27 วันที่ผ่านมา

    whoaaaa!!! 1-3 i've known... but 4th one is just awesome...

  • @QuintessentialDio
    @QuintessentialDio 26 วันที่ผ่านมา

    Very helpful ❤

  • @engelys
    @engelys 27 วันที่ผ่านมา

    Eloquent is so great, didn't know just last one, its looks nice)

  • @clintonbrown4235
    @clintonbrown4235 24 วันที่ผ่านมา

    Thank you 😊

  • @pattespattes
    @pattespattes 25 วันที่ผ่านมา

    i did use the optional, but in the model it's look really convenient!

  • @shubhamsahuSD
    @shubhamsahuSD 26 วันที่ผ่านมา

    3 and 4 are my favourite

  • @ShashankShekhar1408
    @ShashankShekhar1408 26 วันที่ผ่านมา

    I have used all 4 ways according to the need.

  • @AMoktar
    @AMoktar 27 วันที่ผ่านมา

    Thanks 🙏

  • @unu4
    @unu4 27 วันที่ผ่านมา

    I only knew the first two. Thanks

  • @GloverHouse
    @GloverHouse 26 วันที่ผ่านมา

    amazing thx

  • @polishguy9255
    @polishguy9255 10 ชั่วโมงที่ผ่านมา

    Thanks, i know 2/4

  • @br41netutorials
    @br41netutorials 27 วันที่ผ่านมา +1

    I use to know the first 3 but not the last one.
    The ternary also works but just makes the code long.
    Like
    $model->relationship->property ? $model->relationship->property : default

  • @mukangangugi9930
    @mukangangugi9930 27 วันที่ผ่านมา

    I knew 1st and 3rd.
    I prefer the last one since you may have several places to query the model thus cebtralizing the logic in the model is easier. Then you may also have many fields to define defaults for.
    Thankyou

    • @learn-web-dev
      @learn-web-dev 27 วันที่ผ่านมา +2

      I also knew only 1 and 3 and I prefer them. Why 4 is not ideal is because using it you are giving the backend data modeling layer a responsability of the presentation layer/frontend. What is displayed should not be decided in that part of the backend. But now we know it can be done and that is very good :)

  • @beginnerdeveloper
    @beginnerdeveloper 26 วันที่ผ่านมา

    4th one is best 👌

  • @bbbbburton
    @bbbbburton 26 วันที่ผ่านมา +1

    bladestan extension for phpstan/larastan would prevent you from shipping this kind of error in the first place, if you run it on a high enough level

  • @cristianganzer2057
    @cristianganzer2057 26 วันที่ผ่านมา

    I used to use the third option, but the second option is really interesting

  • @hossamibrahim7890
    @hossamibrahim7890 26 วันที่ผ่านมา

    Thanks l

  • @akpevweunuavworho8564
    @akpevweunuavworho8564 26 วันที่ผ่านมา

    nice learnt some new ways. For me i use the @ sign when working with blade files like so {{ $post->title }} ({{ @$post->category->name }})

  • @SilverPaladin
    @SilverPaladin 26 วันที่ผ่านมา

    I see this at least once in every day lol haha!

  • @user-qz6oj3tc3z
    @user-qz6oj3tc3z 26 วันที่ผ่านมา

    null operator the same in dart languge i love that

  • @macdanson4430
    @macdanson4430 24 วันที่ผ่านมา

    I liked the 4th

  • @louisevirtudazo2822
    @louisevirtudazo2822 27 วันที่ผ่านมา

    Can also use optional() helper

    • @hazfrd
      @hazfrd 27 วันที่ผ่านมา

      Yes, it is also presented in video.

  • @TheMuddasir99
    @TheMuddasir99 27 วันที่ผ่านมา

    There is also an other way by putting "@" before the variable.
    @$post->title

    • @Niboros
      @Niboros 26 วันที่ผ่านมา

      nice!

  • @ArshAudioBooks
    @ArshAudioBooks 26 วันที่ผ่านมา

    the last one catches all null fields / values right?

    • @LaravelDaily
      @LaravelDaily  26 วันที่ผ่านมา +1

      I can't guarantee 100% but it should, yes.

  • @RogiervanCann
    @RogiervanCann 27 วันที่ผ่านมา +1

    A bit out of scope, but there shouldn't be any posts without a category on database level to begin with due to proper constraints. So maybe this is the fifth way? 😄

    • @LaravelDaily
      @LaravelDaily  26 วันที่ผ่านมา +1

      Yes if you're able to prevent this situation, it's always the best :)

    • @philipjamesajagabos2519
      @philipjamesajagabos2519 25 วันที่ผ่านมา +1

      Constraints sometimes are nullable in the real sense. Let's take a look at the user to phone relationship. A user might exist without a phone at some time and also it might be stolen or damage making the user phone less, this situation will have some users without a phone. Hence, fetching the user with the phone that is deleted or not yet added will definitely throw the null error if getting a non-existing phone name.

    • @RogiervanCann
      @RogiervanCann 25 วันที่ผ่านมา

      @@philipjamesajagabos2519 Excellent example 👍

  • @user-hi9fp3bw9o
    @user-hi9fp3bw9o 26 วันที่ผ่านมา

    I always use ??

  • @stephenngozi6204
    @stephenngozi6204 27 วันที่ผ่านมา

    I prefer the first method

  • @khoaavan8075
    @khoaavan8075 26 วันที่ผ่านมา

    how about in ?

    • @Niboros
      @Niboros 26 วันที่ผ่านมา

      I suppose the id is required for the route of the form, since submitting your form for a NULL catalog does not make sense. So I would say you could add an if-statement around your form to only add the form when needed. However, I suspect this situation should not happen making that if-statement not needed, because this form is probably on an show-route (something like "/catalog/{catalog}/show") and thus your always have a catalog. Ajust to your needed.

  • @shankargorai4428
    @shankargorai4428 27 วันที่ผ่านมา

    with default doesn't work in has many

    • @LaravelDaily
      @LaravelDaily  27 วันที่ผ่านมา +2

      Yes because there should be MANY defaults then (unknown amount), it's not supposed to work.

  • @1234matthewjohnson
    @1234matthewjohnson 27 วันที่ผ่านมา +1

    !is_null()

  • @cardboarddignity
    @cardboarddignity 26 วันที่ผ่านมา

    I use the ?-> on >8.0, or ?? on