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 upload file in php

How to upload file in php ?

September 13, 2021September 29, 2021

In this tutorial, we will learn basic of uploading of a file in php and also basic configuration required for php file upload. Configure the php.ini for large file upload By default php support 2 mb of file upload but in real world we require more than 2 mb. Same…

Read More

Laravel OTP Login and Registration Without Password

September 1, 2022March 16, 2024

Today most of the applications are using laravel otp login and registration without password for ease of login and registration and also provides easy to login without remembering the password each time while login or register. In Laravel there is multiple ways to implement the login and registration like Laravel…

Read More

Enabling and Disabling Debug Mode in Laravel: A Step-by-Step Guide

July 6, 2023March 16, 2024

Debug mode is a useful feature in Laravel that provides detailed error messages and stack traces during development. However, it’s essential to disable debug mode in production to ensure the security of your application. In this blog post, we will walk you through the steps to enable and disable debug…

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

  • 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 Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version