Home / Snippets / Get Specific Column From Laravel Model
Get Specific Column From Laravel Model cover

Get Specific Column From Laravel Model

1.1K

3 years ago

0 comments

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.
notion avatar

Alaz

Week-end developer currently experimenting with web, mobile, and all things programming.

Topics:

Frontend

Resource

Average

Average

Support Us

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

Welcome to PostSrc V3

PostSrc Dark Logo

You have to login to favorite this