Posts Learn Components Snippets Categories Tags Tools new About
/

Get Specific Column From Laravel Model

Learn How to select specific columns in laravel eloquent

Created on Aug 06, 2021

41 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

)