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

preview_player
Показать описание
If you use a relationship in Laravel which may not exist, there are multiple ways to avoid the error on the screen.

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

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 💡

philipjamesajagabos
Автор

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

VipStudio
Автор

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 😂

ArshAudioBooks
Автор

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

sulaimanmisri
Автор

Did not know about default relation attribute .. wow thanks

stephanpaquet
Автор

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

mukangangugi
Автор

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

ggoparent
Автор

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

FririkurEllefsen
Автор

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

pattespattes
Автор

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

DevduttaBain
Автор

I use to know the first 3 but not the last one.

The ternary also works but just makes the code long.

Like

? : default

brnetutorials
Автор

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

surviplays
Автор

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

muhammadsalman
Автор

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

akpevweunuavworho
Автор

No 4 is definitely the GOATed way to handle it

Maniac-
Автор

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

cristianganzer
Автор

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

IlPandax
Автор

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

SilverPaladin
Автор

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

bbbbburton
Автор

null operator the same in dart languge i love that

qzojtcz