Laravel HasMany: Load Only Specific Fields But Don't Skip xxxxx_id
Laravel 2-in-1 quick tip about hasMany relationships.
In-depth Laravel tutorials covering advanced topics, real-world examples, and best practices.
Laravel 2-in-1 quick tip about hasMany relationships.
JSON columns in the database seem like a solution if you're not sure about the structure of data. But quite often, they lead to more problems in the future. In this article, I want to show both good and bad cases of using JSON columns with Laravel examples.
If you have a `hasMany` relationship and want to get the parent record with a total sum of specific children column values and order by it, here's how to do it.
When structuring a database, similar relationships may repeat, like a Task may be assigned to a User, User Group, or User Role. Is it worth thinking about polymorphic? In this example, let's compare the performance and convenience to use.
Laravel Breeze starter kit registers users and log them in automatically, redirecting to the dashboard. But what if you want the person to log in manually, putting their email and password again after registration?
When do you need to create interfaces in PHP/Laravel? Suppose you have a method with different implementations depending on some condition. In that case, it may be a candidate for an interface, with different classes implementing that interface method differently, depending on that condition. Let me show an example.
When writing automated tests, sometimes you want to repeat the same test multiple times on various data inputs. In Laravel, you can do this easily with data providers and datasets. I will show you a PHPUnit and a Pest example.
One of the typical issues with pagination is if you have more filters with GET parameters on your page. How to keep them from disappearing when the user clicks to visit "/?page=2"?
If you have a Laravel many-to-many relationship and need to sort the Collection by the pivot table value, the query may be complicated. Let's take a look at this example.
Imagine you have a Laravel + Vue project with Posts in the DB but need to show only the first 300 characters of content in the Posts list. Where should you perform that slicing - in Vue or Laravel? Let's compare.