Best code snippets, tutorials and more

Coursecomp is your starting point for developing and maintaining your web application.

How to send email with uploaded files attached In Laravel

Laravel tip: if you want to send emails having user uploaded files attached, you don’t need to save the file first. Instead, you just attach the uploaded file with attachData method and that’s it! Here is the example from a Mailable class: Via @ecrmnn.

How to use whereColumn() method in Laravel

Quick tip: you can use a pretty handy Eloquent method called whereColumn() to verify if columns are equal. For example, you can find posts that have been updated or posts which have the same title and slug. Here is the code: Read more in the official documentation here.

How to reduce code duplication with relationships in Laravel

Since Laravel 9.16 we can reduce the code a bit, using withWhereHas() method. It’s time to do some code clean up! See the example below: As Laravel contributor Eusonlito describes: There are a lot of times that we need to load a relationship using with with the same conditions that we use for whereHas. This method simplifies this […]

How to Add Values to the Form Request After Validation in Laravel

Sometimes it’s necessary to add more values to the form request after it has been validated. These values get returned in this method even though they were not part of the request. For this, we can use the method validated() in our form request and merge the original request data with our custom data. In […]

How to filter by NULL in Eloquent Collections Laravel

Eloquent collections are one of the most powerful tools in the Laravel toolbox. They give you a fluent, convenient wrapper for complex SQL queries. You can chain together different conditions to create complex queries and then call methods on the resulting collection to fetch your data. And here comes the trap. For example, you filter […]

How to use Laravel dd() method with less code

Hello, guys! Did you know that you don’t have to wrap your Eloquent query with dd() method, and just simple can add it at the end of the query? See how simple it is. How to use dd() in Laravel What is dd() in Laravel? Dump and Die, or dd(), is a Laravel helper function […]

How to add custom CSS rules in Tailwind CSS Typography plugin

Sometimes, it is necessary to add some styling to your Tailwindcss .prose class. You can do that in two ways. The first one is obvious: you add some additional CSS classes. Imagine you need to change your headings and paragraphs text to black color. Then, you suddenly realize that also you have to change your […]

How to update or create elements with upsert method in Laravel

If you want to update or create multiple items at the same time, you can use the upsert method: The method’s first argument consists of the values to insert or update, while the second argument lists the column(s) that uniquely identify records within the associated table. The method’s third and final argument is an array […]

29 Best Laravel Tutorials and Resources for Beginners in 2022

Laravel is a free, open-source PHP web framework created by Taylor Otwell. It has expressive, elegant syntax and takes the pain out of development by easing everyday tasks used in most web projects, such as authentication, routing, sessions, and caching. Whether you’re just getting started with Laravel or looking for some advanced topics, this comprehensive […]