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

How many type of methods in Restful api?

August 28, 2021February 22, 2024

Restful api is very useful today while creating a mobile application or web application. Restful api or Http request methods There is 4 types of method of http request. Post Get Put Delete Patch 1. Post method Post method is widely used to fetch the content of a form or…

Read More
Php Facebook Login or Signup in laravel 8 9

How to Implement Facebook Login or Signup in laravel 8 / 9 ?

March 19, 2022March 19, 2022

Facebook login or signup in laravel are demanding from start as it gives user to access the website in single click. Facebook is widely used social media platform therefore most of the users are on it and we can take leverage of it by providing the feature of login and…

Read More
Php How to drop foreign key in laravel migration

How to drop foreign key in laravel migration ?

September 28, 2022March 16, 2024

Drop foreign key to existing table are easy as creating a foreign key and adding columns to it. In laravel migration we can drop foreign key in laravel migration to existing table using dropForeign method of migration class. 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

  • 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