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 How to Restore Soft Deleted Records in Laravel 9

How to Restore Soft Deleted Records in Laravel 9 ?

June 16, 2022June 15, 2022

In this article we will learn to restore soft deleted records in Laravel. In our recent article How to fetch Soft Deleted Records in Laravel 9 ? we learnt to fetch the records from database which is soft deleted and sometimes we want to restore records that are soft deleted…

Read More
Php How to Set and Get Cookies in Laravel

How to Set and Get Cookies in Laravel ?

July 25, 2022July 25, 2022

In this article we will learn to set and get cookies in laravel. Cookies are used to store the data in client computer, Cookie can hold tiny information in computer and can retrieve when required for same website. In laravel we can store and fetch cookies using laravel inbuilt methods…

Read More
Php How to add column in laravel migration

How to Add Column in Existing Table Laravel migration ?

February 15, 2022June 30, 2022

Adding column to existing table are easy as creating a new table and adding columns to it. In laravel migration we can add new colum to existing table using the same method we used in create migrations . Major difference between creating new table and updating new table is Schema::create…

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