Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Get Specific Columns Using with() function in laravel

How to get specific columns using with function in laravel ?

Aman Jain, November 8, 2023March 16, 2024

When working with Laravel, a popular PHP framework, you’ll often need to retrieve specific columns using with() function in laravel from your database tables. Laravel provides a powerful and efficient way to do this using the with() function. This function allows you to specify which related models and their columns you want to retrieve, providing you with fine-grained control over your data retrieval. In this comprehensive guide, we will explore how to get specific columns using the with() function in Laravel.

Below examples will work with any version of laravel like laravel 5, laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10.

Retrieving Specific Columns Using with function in laravel

There is two methods to get sepecific columns using with() function. First one work well with with if we do not want to add mutliptle relation as below

$posts = Post::with('comments:id,body')->get();

//
$posts = Post::with(['comments:id,body','likes:id,user_id'])->get();

Second methid to retrieve specific columns with the with() function, you can pass an array of column names as the second argument for the related model. Let’s see how this works with an example:

$posts = Post::with(['comments' => function ($query) {
    $query->select('id', 'body');
}])->get();

In this example, we retrieve all posts along with only the id and body columns from the comments table. The $query parameter within the closure allows you to define the columns you want to retrieve for the related model. This is particularly useful when you want to reduce the amount of data fetched from the database, improving the performance of your application.

Chaining with() for Multiple Relationships

Laravel’s Eloquent ORM provides the flexibility to chain multiple with() functions to retrieve columns from various related models in one go. This can be incredibly helpful when dealing with complex data structures and relationships. Here’s an example:

$posts = Post::with([
    'comments' => function ($query) {
        $query->select('id', 'body');
    },
    'author' => function ($query) {
        $query->select('id', 'name');
    }
])->get();

Conclusion

Laravel’s with() function is a valuable tool for retrieving specific columns when working with related models. It empowers you to optimize your database queries, reduce data transfer, and enhance the performance of your application. By understanding the syntax and possibilities of the with() function, you can efficiently fetch the data you need, whether it’s for simple one-to-one relationships or complex many-to-many associations

How to print raw sql query in eloquent laravel ?

Related

Laravel columnslaravelwith

Post navigation

Previous post
Next post

Related Posts

Php laravel custom validation

How to make custom validation in Laravel 8 ?

October 19, 2021October 28, 2021

In this tutorial we will learn to implement custom validation rule in Laravel 8. Laravel has too many validation rules but sometime we feel some rules are missing according to our project need. so, in this article we will learn to implement custom reusable validation rule and implement the username…

Read More
Php How to Upload image with preview in Laravel 8 with example

How to Upload image with preview in Laravel 8 with example ?

May 14, 2022August 20, 2022

Upload image with preview in laravel or for any website can be basic requirement like set up a profile picture to providing the documents. Laravel provides robust functionality to upload and process the image with security. Laravel simply use Request file method to access the uploaded file and then we…

Read More
Php How to Add Values to Request Array in Laravel

How to Add Values to Request Array in Laravel ?

August 9, 2022March 16, 2024

In laravel to get the request data we use request class or function but sometimes we want to add values to request array in laravel, so to add extra values or we can say to merge to our own custom values to laravel request array we need to use merge…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version