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 Use Soft Delete to Temporary (Trash) Delete the Records in Laravel

Use Soft Delete to Temporary (Trash) Delete the Records in Laravel ?

June 17, 2022June 17, 2022

In laravel we use Soft Delete to Temporary (Trash) Delete the Records. Soft delete in laravel is use to hide the record from users by keeping the data in the database. There is many purpose of soft delete like deleting the user for admin but keep all the information in…

Read More
Php How to Prepend or Append File Content in Laravel

How to Prepend or Append File Content in Laravel ?

May 29, 2022May 29, 2022

In our last article I showed you to create file and this article will cover to Prepend or Append File Content in Laravel application. To perform the file system based operations like create, replace, prepend or append File Content file in laravel, we use File class or Storage class in…

Read More
Php Creating resource controller in laravel

How to create resource controller in Laravel 8 ?

November 7, 2021November 7, 2021

Resource controller is helpful when you want Create, Read, Update and Delete (CRUD) operations. If you have a model with same set of actions like crud then resource controller are useful. we can create resource controller easily using the artisan command. Above command will generate controller ImageController in \app\Http\Controllers. As…

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

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

  • Understanding High Vulnerabilities: A Closer Look at the Week of July 14, 2025
  • Exploring Fresh Resources for Web Designers and Developers
  • The Intersection of Security and Technology: Understanding Vulnerabilities
  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version