How to Automatically Return JSON for Laravel Eloquent
When working with APIs, you often need to return an Eloquent response in JSON format. By default, Laravel transforms the response into JSON if you just return a Model or Eloquent Collection.
We found 20 results for "eloquent tips".
When working with APIs, you often need to return an Eloquent response in JSON format. By default, Laravel transforms the response into JSON if you just return a Model or Eloquent Collection.
When counting the Model records grouped by their type in a relationship, it's tempting to load too many DB queries or too much data into the memory. There are a few ways to optimize it, let's take a look at an example.
Imagine you want to load the Model with its related many models, but sort those related results by some column in that related DB table. How to do that?
This video is a 4-in-1 lesson from my course "Eloquent: Expert Level" about N+1 Query Problem.
Let's get a bit deeper into the topic of Soft Deletes. How can you query/restore the soft-deleted records? How do they work with Observers? How to cascade the soft-deletes?
While optimizing Eloquent and DB performance in Laravel projects, it's not always about the number of queries or slowness of an individual query. There's one more important thing, let me show you, with an example.
To calculate the related records, we use withCount() in Eloquent. But there are a few "advanced" features I want to show you today.
How to store query results into the cache for some time, so Laravel wouldn't even hit the database?
A simple example of how to use less memory when loading Eloquent data.
Answering a question from Twitter: get the categories with their products but only 4 products per category. How to achieve it?
I want to show the example of two ways to use Eloquent scopes in Laravel.
This video is based on a tweet by Matt Kingshott: he emphasizes that the DB index is not only important to be created but to actually be USED.
If you never used $appends in Eloquent Models, this video is for you.
This short lesson will be both about Laravel and about general software development. One of the most often and common mistakes made by developers is not checking input data. And then not only you get random errors of something "not found", but sometimes much worse - expose your system to vulnerabilities and attacks. So let's discuss that with examples from Eloquent world.
When creating API applications, you often don't want to return ALL the data via API, especially sensitive fields like passwords. In this short tutorial, I will show 4 methods to return only the fields which you need.
Today I want to briefly overview one package Laravel-Excel. I probably don't need to explain its purpose - the title says everything. But what I do want to show you is how easy it is to export data to Excel using Eloquent.
Eloquent has a convenient feature called Accessors - you can define your own custom fields on top of existing in the database table. But then there's an Eloquent property $appends - should we use it or not? And what's the difference?
I know the title sounds unclear, so let me start with an example. What would be your Eloquent query if you have birth_date field in DB an want to show how many of your users are adult 18+ years, and how many are still children? Let's take a look.
Another "hidden gem" of Laravel which is surprisingly rarely used or even known, though it's mentioned in the official Eloquent documentation. Imagine that you have a record, and you need to check if that record already exists in the database - to prevent duplicate, you wouldn't save it second time. There's an elegant way to perform it in Eloquent.
Eloquent has one less-known function called withCount(): it helps to get the amount of related records inside of the main object. It also works with two layers deep, inside of hasManyThrough relations. Let's take a look.