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 - แนวปฏิบัติและการใช้ชีวิต
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 💡
Exactly the same here :) I love 4th approach 🤘🏻🍻
No 4 is definitely the GOATed way to handle it
Did not know about default relation attribute .. wow thanks
Honestly, I have also seen for the first time
I learned alot as always, but I must admit that I would have used forelse and then empty
I love the 4th method. It's new to me and I no need to code that null safe operator again and again.
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 😂
It is really helping I did not know these before.❤
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
also you can do @ as well in front of variable like @$post->category->name you can try that.. i hope it would help.
Thanks! I didn't know the 4th. Great video! ;)
whoaaaa!!! 1-3 i've known... but 4th one is just awesome...
Very helpful ❤
Eloquent is so great, didn't know just last one, its looks nice)
Thank you 😊
i did use the optional, but in the model it's look really convenient!
3 and 4 are my favourite
I have used all 4 ways according to the need.
Thanks 🙏
I only knew the first two. Thanks
amazing thx
Thanks, i know 2/4
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
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
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 :)
4th one is best 👌
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
I used to use the third option, but the second option is really interesting
Thanks l
nice learnt some new ways. For me i use the @ sign when working with blade files like so {{ $post->title }} ({{ @$post->category->name }})
I see this at least once in every day lol haha!
null operator the same in dart languge i love that
I liked the 4th
Can also use optional() helper
Yes, it is also presented in video.
There is also an other way by putting "@" before the variable.
@$post->title
nice!
the last one catches all null fields / values right?
I can't guarantee 100% but it should, yes.
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? 😄
Yes if you're able to prevent this situation, it's always the best :)
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.
@@philipjamesajagabos2519 Excellent example 👍
I always use ??
I prefer the first method
how about in ?
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.
with default doesn't work in has many
Yes because there should be MANY defaults then (unknown amount), it's not supposed to work.
!is_null()
I use the ?-> on >8.0, or ?? on