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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys probably you have seen this error at least once in your laral career property on null which often comes from the relationship something like this in Blade you load the relationship and that object does not exist anymore for whatever reason and in this video I will show you four different ways how to avoid this error on the screen you probably know some of those ways but do you know all of them after watching this video share in the comments below the setup of this demo project is very simple the model category which has soft deletes and this is important the model post with category relationship belongs to and then just a simple controller with one method invocable controller with just listing all the post with their categories and the blade you saw that one already and if all categories and posts exist successfully this is the success page without any design I didn't spend time on building any design but what if I soft delete one of the categories for example here in my tinkle well it returns true and in the database I refresh the page and have deleted ad for the business category now we will have obviously an error that there's no business category anymore okay now let's go to those four ways to fix that the first way is to use so-called PHP null safe operator which means just adding question mark here as a result if this is empty it will return empty and will not even bother calling the name so if we refresh now no error and the category is just empty so this is probably the easiest fix it appeared in PHP 8 but if for some weird reason you are on the older version you may want to use laravel helper called optional so larel has something like this so optional object and that will also default to empty and will not throw any errors so that was way number two I used to use optional personally in the past but since PHP 8 I prefer native n safe operator of PHP now way number three what if you want to have default value here instead of just empty string and again we can do that in PHP so we return that string but if it doesn't exist two question marks and have something here it may be empty string which refresh and that doesn't change but we can then do no category or whatever you want to put in here so that is not just for avoiding the error but also adding a logic of what should be the default here so that was way number three and then way number four is an eloquent model so while defining post category in the model here you may specify a thing called with default so if that relationship doesn't exist for whatever reason then then you may add with default and then you don't need to change anything here in the blade the blade stays like in the initial State before this video we refresh and there's no error here anymore and even that is not all as a way for 4B let's call it with default also accepts array as an optional parameter where you can Define the default values of certain fields for example category name could be no category here in the model level and then again we don't change anything in the blade we refresh the page and we see no category here so yeah these are four ways how to avoid that typical laral error with relationships did you know them all which one do you prefer let's discuss in the comments below and if you want more eloquent tips like this one we pretty recently updated the course called laral 11 eloquent expert level with a lot of small or big pieces of advice about models relationships and stuff like that so I will link that course in the description below that's it for this time and see you guys in other videos
Info
Channel: Laravel Daily
Views: 8,471
Rating: undefined out of 5
Keywords:
Id: Gq60Ms99bkg
Channel Id: undefined
Length: 4min 10sec (250 seconds)
Published: Mon Apr 22 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.