Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks

how to add multiple select in laravel eloquent ?

Aman Jain, December 29, 2021February 22, 2024

Laravel eloquent builder has rich features to modify the query. In some cases we wanted to add multiple select in laravel and modify the select statement on basis of few conditions and wanted to add some select statement on condition. so in this article we will learn to add multiple select in laravel eloquent by preserving the last select statement.

By default laravel selects all columns by executing the eloquent query using method first, get, paginate etc and we can also tell the eloquent query builder to which column to select using select method.

In this tutorial i will use addSelect to add the multiple select statement in query builder.

Let’s understand it with examples

Example 1

In this example i will create a table article in database and will create a model or QueryBuilder then will add multiple select using addSelect method.

 $query=\DB::table("articles")->select("*")->where("title","test");
 dd($query->toSql());

Output of above script will be

"select * from `articles` where `title` = ?"

Let’s add more select statement

 $query=\DB::table("articles")->select("*")->select("1 as number")->where("title","test");
 dd($query->toSql());
 
  //or 
$query=\DB::table("articles")->select("*")->where("title","test");
if($type=='PUBLIC'){
  $query = $query->select("1 as number");
}
dd($query->toSql());
 

Output will be

select `1` as `num` from `articles` where `title` = ?

As you can see now it has removed the * so in this case we need to add addSelect here

 $query=\DB::table("articles")->select("*")->addSelect("1 as number")->where("title","test");
 dd($query->toSql());
 
  //or 
$query=\DB::table("articles")->select("*")->where("title","test");
if($type=='PUBLIC'){
  $query = $query->addSelect("1 as number");
}
dd($query->toSql());
 
beenhere

addSelect

\DB::table(“articles”)->select(“*”)->addSelect(“1 as number”)->where(“title”,”test”)

How to add select in with relation in Eloquent ?

Sometimes we wanted to add select in our relational statement using with to get the specific columns in relational query so we can add that in below way

$query=Article::with(["user"=>function($q){
     $q->select("name");
}])->where("title","test")->get();

Here, Article is model name and user is relation with article.

How to add select in subquery in Eloquent ?

In the same way we can add select statement in subquery as we did in relational query.

$query=Article::whereIn("type",function($q){
     $q->select("name")->from('users')
                        ->where('type','ADMIN');
})->where("title","test")->get();

Related

Php Laravel Mysql Eloquentlaravelphp

Post navigation

Previous post
Next post

Related Posts

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 make a component in laravel 10

How to create component in Laravel 10 ?

March 23, 2023March 16, 2024

In this tutorial i will show you simple html based component in Laravel 10 using blade. Creating reusable components is a fundamental aspect of modern web development, as it allows developers to simplify code and improve efficiency. Laravel 10, one of the most popular PHP frameworks, comes with a robust…

Read More
Php laravel routing

Laravel 8 routing with example

October 5, 2021January 13, 2022

In this post we will learn to implement the Laravel 8 routing and how it’s different from Laravel other versions. Laravel routing is used to create the URL for each POST, GET requests and it works as a bridge between the controller or html view. In this article we will…

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