Press "Enter" to skip to content

Five nightmares in laravel for its developers

Sharing is caring!

I have been doing this for 3 years now but these errors still haunt me.

If you do not find any mistake on that code, then you must be new to laravel.

As laravel allows us to develop prototype rapidly, I often run to this problem where I forget to return the relationship. The reason I am complaining here is, this situation triggers an error but it does not help us in any way. We just have to figure out what went wrong.

As you can see in the picture, it does not show which file caused the error.

If this has occurred to you before, you would know that you forgot to put the ‘return’ in relationship declaration you might have just declared.

If not, you are in for a treat.

  • Keeping name same in multiple classes

With Laravel, I sort of like to put things in proper place as much as possible, if the project is new. So, doing major operation in controller and dispatching event is pretty much a pattern for every operation.

For example:

But, now we have to create a listener and a mail class to send the email by listening to an event. We can do it right there from the controller but I feel like that won’t be proper.

The trick here is done by IDE. As we use shortcut keys to import classes, there is exact 50% chance of importing the wrong class.

And once it triggers, it takes ages to figure out that it was supposed to be the class with same name but from different namespace.

Using dd() helper function is not helpful at all.

It tends to happen while using repository pattern too.

  • Mass assignment errors

Mass assignment errors are another errors that often bothers when prototyping.

Often times we want to do this. But, it triggers Mass Assignment exception.

So, after looking at errors, we open model files and fillable properties.

  • Using env() directly

In often case, I forget that env() function is not meant to be used directly in your production application.

If you execute the config:cache command during your deployment process, you should be sure that you are only calling the env function from within your configuration files. Once the configuration has been cached, the .env file will not be loaded and all calls to the env function will return null.

  • Problem with Seeder

If you use seeder, you might have run into this problem.

If you create a new seeder file just by copying and renaming an old one and try to call that seeder than we get reflection exception.

The db:seed command fails with this reflection exception. It basically means, class not found. In that case you just have to run this command.

This situation though, never occurs if I create file using

Let me know if you face similar problems in other parts of laravel.

Everything is awesome.

Comments are closed.