Posts Learn Components Snippets Categories Tags About
/

Get Specific Column From Laravel Model

Learn How to select specific columns in laravel eloquent

Created on Aug 06, 2021

18 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.

Snippets by

PostSrc poster

PostSrc

Full stack developer mainly focusing on the front end side of the web. The main author of PostSrc and loves writing tutorial and guides online.

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's right for your project.

View Components

Other Code Snippets

More