Posts Learn Components Snippets Categories Tags Tools About
/

Get Specific Column From Laravel Model

Learn How to select specific columns in laravel eloquent

Created on Aug 06, 2021

1073 views

When getting all of the model instances you can specify the column that you want to retrieve by passing an array in the "all()" method.

By doing so you will have only the column that you need reducing the amount of data that you have to retrieve and possibly preventing any unnecessary data.

Laravel Model Get Specify Column Example
<?php

$posts = Post::all(['id', 'title', 'slug']);

When you specify the code like above you will only get the "id", "title", and "slug" from all of the model instances that is being retrieved.

Do note that the code above can be optimized using "cursor()" and "chunk()" for better optimzation.

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

Load comments for Get Specific Column From Laravel Model

new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that are right for your project.

View Components

Sponsors 👑

+ Add Yours
)