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

Laravel folder paths using helpers in laravel

How to get folder paths using helpers in laravel?

August 27, 2021February 22, 2024

Laravel has helpers to get folder paths using helpers in laravel and by which we can easily get root folder, public folder, assets folder, storage folder, app folder etc. Laravel has helpers to get the path of root folder, public folder, assets folder, storage folder, app folder. To get the…

Read More
Php Laravel Ajax pagination example with search

Laravel Ajax pagination with search

November 27, 2021December 4, 2021

In this tutorial we will learn pagination in Laravel using Ajax jQuery. Laravel provides its own library to build the pagination html, which we can easily use in our html page using $model->links() method and $model->paginate() method to make a long list into pagination. But sometimes our requirements are different…

Read More

Laravel array validation with example

December 15, 2021November 12, 2023

In this tutorial i will explain to use validating a array inputs using Laravel validation library. Laravel also supports to validate group of array and we can validate a array same as normal input. In our recent article Laravel form validation how to validate a form and files. In this…

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

  • July 2025
  • 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

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version