Laravel Pro Tips
If you ever need to build a quick CSV export command for your Laravel application.
This is how I would recommend doing it. Open a resource to StdOut and then chunk your results and write them to the output using fputcsv(). It'll handle the CSV output perfectly 👌
💡 #Laravel Tips of the day 😎
𝗗𝗼𝗻’𝘁 𝗙𝗶𝗹𝘁𝗲𝗿 𝗯𝘆 𝗡𝗨𝗟𝗟 𝗶𝗻 𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗶𝗼𝗻𝘀: We can filter by NULL in Eloquent, but if we're filtering the collection further - filter by empty string, there's no "null" in that field anymore. #php #backend
You've probably joined an array into a comma separated string before. But what about when you want the last one to say "and"? With Laravel collections it's a breeze 🍃
If you have a collection, and you want to get items from the end of it, you may pass a negative integer to `take()` method to take the specified number of items from the end:
Here is an example:
🔥 Laravel Tip: If you have an eloquent collection, and you want to update every model.
You can use the `toQuery` method, and execute the update with only one query instead of looping through the collection.
The every method may be used to verify that all elements of a collection pass a given truth test.
Laravel Collections tip. To remove all falsy values from the collection, simply call filter without any arguments ✨
👌🏻 Laravel tip of the day
You can use the "higher order messages" to refactor the logic that returns custom fields to a separate model method and feed it to the other collection method. For instance, the `map` method.
This makes things tidier while building queries! #Laravel
Just discovered that there is a method to check whether a Laravel collection contains exactly one item or not. I've written my last count() === 1 😅
No articles were found.