Select only the columns you need in Laravel
Usually to retrieve results from a database table, we would do the following.
The above code will result in a query as below
As you can see, the query is doing a select *
. This means it is retrieving all the columns from the database table.
This is fine if we really need all the columns from the table.
Instead, if we need only specific columns(id, title), we can retrieve only those columns as below.
The above code will result in a query as below