To get your latest posts ordered by year, you can use groupBy in your query. See the example below:
Coursecomp is your starting point for developing and maintaining your web application.
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.
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.
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 […]
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 […]
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 […]
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 […]
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 […]
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 […]
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 […]