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
5
votes
2 answers
Laravel 5.4: Directly Subtract number from column in database
I want to subtract 100 number from credit column in database that is having int data type. I am looking for the way to directly subtract it with some Laravel query. But right now I first get the column value from the table and then subtract and then…

Amit Gupta
- 2,771
- 2
- 17
- 31
5
votes
6 answers
raw SQL to laravel query
Can anybody please help me to write a DB query version of the SQL statement below.
I need a little help around the select statement and the partitioned joins.
I have managed to do this so far.
$query = DB::table(raw('SapakInAdminOrder…

Wdy Dev
- 219
- 2
- 11
5
votes
2 answers
laravel paginate when use select case and parameter binding
Laravel Version: 5.5
PHP Version:7
Hi, I want to execute this query:
select (case
when(title like 'my-keyword') then 1
when(description like 'my-keyword') then 2
) as ordering from products where id > 10;
When I do this by query…

Roham Shojaei
- 440
- 4
- 18
5
votes
2 answers
Running raw sql inside Model : Laravel
I am using Laravel and need to run raw sql query from a Model/Repository class
INSERT INTO calendar
(room_id, date, default_count, default_price)
VALUES ('1', '2017-01-02', '2', '400004')
ON DUPLICATE KEY UPDATE
default_count =…

Dumindu Madunuwan
- 411
- 5
- 13
5
votes
1 answer
How to update time to NOW()?
I want to set a date field to NOW() if it's outdated. Here is the query I'm trying to run without success:
Quiz::('date', '<', DB::raw('NOW()'))->update(['date' => DB::raw('NOW()')])
How do I fix it?
P.S
I don't want to deal with Carbon, if…

Alex Lomia
- 6,705
- 12
- 53
- 87
5
votes
1 answer
Laravel: change a raw query in a "query-builder" or "eloquent" one
I have this Laravel Query Builder snippet that is working fine:
$records = DB::table('users')
->select(
DB::raw('users.*, activations.id AS activation,
(SELECT roles.name FROM roles
INNER JOIN…

Ivan
- 2,463
- 6
- 39
- 51
5
votes
1 answer
Get Data from Multiple Tables Laravel
I am trying to build a coupon site in Laravel. Each merchant has their own deals/coupons. I have been able to print deals/coupons for a merchant on their specific pages.
Here's my query
$deals = DB::table('deals')
-> join ('merchants',…

Gaurav Mehta
- 1,103
- 4
- 16
- 27
4
votes
1 answer
how to get updated records ids in laravel?
I am updating multiple rows in laravel .
StoragePeriod::where('customer_id', $invoice->customer_id)->update(['invoice_id' => $invoice->id]);
How I can get ids of those updated records?
user13815476
4
votes
6 answers
How can i paginate laravel collection?
here is how i trying to paginate:
$posts = Post::all()->sortByDesc("created_at")->pagination(1);
but i get this error:
Method Illuminate\Database\Eloquent\Collection::pagination does not exist.

Jetz
- 207
- 2
- 4
- 14
4
votes
3 answers
Why is PHP using a lot of memory to store a query result
I use Laravel 8 to perform a query on a MySQL 8 table using the query builder directly to avoid Eloquent overhead but I'm getting a lot of memory consumption anyway.
To show you an example, I perform the following query to select exactly 300 000…

Marc
- 1,350
- 2
- 11
- 29
4
votes
1 answer
Laravel Eloquent equivalent of a QueryBuilder query
I have a Laravel 7 project and this database structure:
races participants bibs coords
------- -------------- ------ --------
id id id id
race_id bib_id
…

Marc
- 1,350
- 2
- 11
- 29
4
votes
1 answer
laravel addSelectRaw() - how to bind a variable in addSelect()?
How can I addSelectRaw() in order to bind my variables to addSelect()?
I've got this in my code:
$query->addSelect( DB::raw('MATCH(matchy.val) against ("'.addslashes($q).'") as relevance ') );
addslashes() is far less than ideal, and i should be…

iateadonut
- 1,951
- 21
- 32
4
votes
1 answer
Laravel lookup avg result in other table
I've got this query in Laravel:
DB::table('score')
->select('score.score_nl', DB::raw('count(*) as total'), DB::raw('round(avg(rating_results.rating)) as final_rating'))
->join('rating', 'rating.score_id', '=', 'score.id')
…

Jenssen
- 1,801
- 4
- 38
- 72
4
votes
1 answer
How to automatically add a value to an existing value in Laravel
How can I run this SQL statement with Laravel Eloquent ORM using my model?
update products set quantity = quantity + 3

Shynkk
- 43
- 4
4
votes
1 answer
laravel: JSON Field Comparison with Array
laravel-5.7/mysql
In my database, I have a json format field like this:
field name: features:
[
{"id": 1, "url": null, "name": "A"},
{"id": 2, "url": null, "name": "B"}
]
Also in its model,I wrote this
protected $casts = [
…

Areza
- 671
- 14
- 26