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 install laravel composer

How to install laravel 8 ?

September 20, 2021May 6, 2022

Laravel is most popular framework of PHP. It provides lots of in-build tools and library to build the web application. Laravel is used to to create robust custom web applications with ease of amazing developer experience. Laravel is easily customizable and provides tools like ORM, Validation libraries, Queue, Mail, Routing,…

Read More
Javascript Laravel Customized Autocomplete JQuery UI

Laravel Customized Autocomplete JQuery UI

July 12, 2022July 12, 2022

Laravel Customized Autocomplete JQuery UI is useful when we want live search of bulk data. Autocomplete search is mostly work of javascript and when we want to fetch live data from database then we require the intervention of laravel to provide the data in json response. In this tutorial we…

Read More
Php Laravel encrypt and decrypt strings

How to encrypt and decrypt string in laravel 8 ?

January 26, 2022January 26, 2022

Laravel provides inbuilt library to encrypt and decrypt the strings. Laravel usage OpenSSL to provide AES-256 encryption which creates long encrypted string. Encryption and decryption is a technique to hide the useful some information in a form which is not readable. In this tutorial i will show you to encrypt…

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