The database query builder provides a convenient, fluent interface to creating and running database queries in Laravel apps. It can be used to perform most database operations in your application, and works on all supported database systems.
Questions tagged [laravel-query-builder]
1522 questions
825
votes
40 answers
How do I get the query builder to output its raw SQL query as a string?
Given the following code:
DB::table('users')->get();
I want to get the raw SQL query string that the database query builder above will generate. In this example, it would be SELECT * FROM users.
How do I do this?

meiryo
- 11,157
- 14
- 47
- 52
584
votes
26 answers
How to Create Multiple Where Clause Query Using Laravel Eloquent?
I'm using the Laravel Eloquent query builder and I have a query where I want a WHERE clause on multiple conditions. It works, but it's not elegant.
Example:
$results = User::where('this', '=', 1)
->where('that', '=', 1)
->where('this_too',…

veksen
- 6,863
- 5
- 20
- 33
400
votes
15 answers
Laravel Eloquent Query: Using WHERE with OR AND OR?
How do I say WHERE (a = 1 OR b =1 ) AND (c = 1 OR d = 1)
For more complicated queries am I supposed to use raw SQL?

Farzher
- 13,934
- 21
- 69
- 100
333
votes
20 answers
Get Specific Columns Using “With()” Function in Laravel Eloquent
I have two tables, User and Post. One User can have many posts and one post belongs to only one user.
In my User model I have a hasMany relation...
public function post(){
return $this->hasmany('post');
}
And in my post model I have a…

Awais Qarni
- 17,492
- 24
- 75
- 137
318
votes
5 answers
How to sort a Laravel query builder result by multiple columns?
I want to sort multiple columns in Laravel 4 by using the method orderBy() in Laravel Eloquent. The query will be generated using Eloquent like this:
SELECT *
FROM mytable
ORDER BY
coloumn1 DESC, coloumn2 ASC
How can I do this?

Sophy
- 8,845
- 6
- 36
- 30
188
votes
21 answers
Get the Query Executed in Laravel 3/4
How can I retrieve the raw executed SQL query in Laravel 3/4 using Laravel Query Builder or Eloquent ORM?
For example, something like this:
DB::table('users')->where_status(1)->get();
Or:
(posts (id, user_id,…

Patrick Maciel
- 4,874
- 8
- 40
- 80
186
votes
15 answers
Creating and Update Laravel Eloquent
What's the shorthand for inserting a new record or updating if it exists?
where('metadataKey', '=', 2001)->first();
if ($shopOwner == null) {
// Insert new record into database
}…

1myb
- 3,536
- 12
- 53
- 73
140
votes
9 answers
A JOIN With Additional Conditions Using Query Builder or Eloquent
I'm trying to add a condition using a JOIN query with Laravel Query Builder.

dede
- 2,523
- 2
- 28
- 32
30
votes
1 answer
What is the meaning of Eloquent's Model::query()?
Can anyone please explain in detail what Eloquent's Model::query() means?

Shateel Ahmed
- 1,264
- 2
- 12
- 23
30
votes
3 answers
laravel querybuilder how to use like in wherein function
$book = array('book1','book2');
$book array elements numbers are variable. it might have 2 element or 20 elements
I need to make a query like this:
select * from book where bookname like %book1% or bookname like %book2%
To make this query in…

Al-Alamin
- 1,438
- 2
- 15
- 34
16
votes
3 answers
Laravel upsert operations with Query Builder
In one of the my worker scripts to store aggregate counts based on some metrics, I am not using Eloquent as the queries are a little complex and and it is easy to write using query builder. I am currently getting the values from the database and I…

Happy Coder
- 4,255
- 13
- 75
- 152
14
votes
2 answers
Laravel Query Builder, selectRaw or select and raw
What's the difference between:
DB::table('some_table')
->selectRaw('COUNT(*) AS result')
->get();
and:
DB::select(DB::raw("
SELECT COUNT(*) AS result
FROM some_table"));
In the documentation https://laravel.com/docs/5.6/queries they advert about…

pmiranda
- 7,602
- 14
- 72
- 155
12
votes
2 answers
Laravel 5.6 Polymorphic relation with whereHas
I am facing an issues in Polymorphic relation where I can not make whereHas to work. Basically I have a "where" condition which i want to apply.
The relation code is working fine to return the related models but it returns errors once applying the…

Hadi.M
- 544
- 1
- 6
- 19
12
votes
2 answers
Laravel passing variable to wherehas query
I want to pass variable to wherehas query in laravel.. but getting an error of undefined variable,
In method, if has nature then go where has natures equal to $catname...
in line no. 4
public function Products($catname,Request $request) …

Manish Arora
- 259
- 1
- 4
- 11
12
votes
5 answers
How to change date format in laravel Query Builder from "2016-03-12" to "12-Mar-2016"
How to change date-format in laravel from "2016-03-12" to "12-Mar-2016"
$results = DB::table('customers as cust')
->where('cust.id',$id)
->select("cust.*","cust.cust_dob as dob")
->first();
Should I use laravel raw query.
I…

GRESPL Nagpur
- 2,048
- 3
- 20
- 40