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
-2
votes
2 answers
Does DB raw sum function help optimized data?
In the application, we have a feature where it will display all the years as tab. Each tab has amount(where the amount is the summarized) but at the same time it is have data.
Scenario
Just to give a background about the application, it is a car…

Angel
- 966
- 4
- 25
- 60
-2
votes
1 answer
Case statement with withCount in Laravel
How do I achieve this kind of query in Laravel?
Model::query()
->select('id')
->withCount(['relation_1', 'relation_2', 'relation_3'])
->selectRaw(
'(CASE
WHEN relation_1_count < 0 THEN "relation 1 failed"
WHEN…

Suresh Hemal
- 40
- 1
- 5
-2
votes
2 answers
Query database table
I have a table where I save plays for players over a specific game on it.
the game has different levels.
the player can play the same level multiple times.
I need to query that table to count the number of levels that a specific player played today…

Bebo
- 5
- 2
-2
votes
1 answer
How to retrieve records by as a range (As1-As10) in laravel?
is there any possibility to retrieve records as a range.For an example there is a table with column as ref_numbers.Sample values for the ref_numbers are A1,A2,A3,A4....AB1,AB2,AB3,AB4...
I want to retrieve the records of specific range starting…
-2
votes
1 answer
how to convert SQL query in Laravel Query Builder
SQL Query is
SELECT
*,
(
SELECT COUNT(*)
FROM `users_data`
WHERE `users_data`.`role` = `user_role`.`role_id`
) AS `total_users`
FROM `user_role`
WHERE `user_role`.`role_type` = 'USER'
ORDER BY…

Durgesh Verma
- 13
- 1
-2
votes
1 answer
How to do a JOIN ON CASE with laravel query builder?
Is there a way to translate the following syntax to laravel query builder?
SELECT *
FROM table_1
INNER JOIN table_2 ON table_1.id = table_2.id
INNER JOIN table_3 ON
(CASE
WHEN {some condition}
THEN table_1.id
ELSE table_2.id
END) =…

Mark Adel
- 223
- 1
- 10
-2
votes
1 answer
Laravel: Query builder
Might be stupid question, how to correctly state the ID in select statement?
public function generatefinalconfirmation($id) {
$booking = Booking::findOrFail($id);
$flightin = DB::select('SELECT b.bookingname, f.origin FROM Bookings B…

doom
- 11
- 1
- 3
-2
votes
2 answers
Laravel: Array to String
It is my code in which I get the subcategories' names. But the problem is it shows data in an array form.
$couponCategory = Coupon::select('categories')->where('expiry_date', '>', $dt)->where('status', 1)->first();
$couponsCat = explode(',',…

Muqadar Ali
- 57
- 2
- 11
-2
votes
1 answer
How to get ordered this sql query
comments table
comments table
stars table
stars table
i write this query: (for ordering sites by most stars_sum)
$comments->join('stars', 'comments.star_id', '=', 'stars.id');
$comments->selectRaw('comments.site_id as site_id, comments.id as…

JalalLinuX
- 1
- 1
-2
votes
1 answer
Laravel query builder search by status
In my project, the user can filter the data according to 3 criteria (declared, in progress, finished).
I pass string, in the url parameters [-1,0,1], in order to obtain the active filters. the statuses are defined according to several data coming…

Larave
- 13
- 2
-2
votes
1 answer
Variable inside laravel query builder is always null
Im trying to find the users that has commented the product this way:
public function getUserToRate(Product $product)
{
$userIds = DB::table('product_ratings')->where('product_id', $product->id)->pluck('user_id');
return $userIds;
}
But the…

Alberto
- 61
- 1
- 9
-2
votes
2 answers
Convert raw SQL query to Laravel DB builder query
I need help converting the following raw query into a Laravel query using the DB query builder. The query must be built using the DB (facade) query builder and not Eloquent.
SELECT distinct p.client_id
FROM people p
JOIN (SELECT client_id,…

Riz
- 71
- 2
- 10
-2
votes
1 answer
How to Convert this Query to Eloquent or Query Builder?
I have a query like this. How should I convert it into a eloquent or query builder
SELECT
x.MATERIAL_ID,
(SELECT TAPET_NAME FROM MA_TAPE_TYPE WHERE TAPET_CODE = x.MATERIAL_TYPE) as media_type,
(SELECT TAPEF_NAME FROM MA_TAPE_FORMAT WHERE TAPEF_CODE…

elfishy97
- 1
- 1
-2
votes
1 answer
find nearest location using lat and long in laravel query bulider
$lat = isset($request->lat) ? $request->lat : '';
$lng = isset($request->lng) ? $request->lng : '';
$query = Vehicle::query();
$query->whereHas('vendor', function($q) use($lat, $lng){
$q->where('vendor_status', 1);
if((isset($lat) && $lat…

divakar
- 61
- 2
- 10
-2
votes
1 answer
How to make tree joins at the same time with Laravel Query Builder?
I tried to make tree joins with this code, but didn't work...
$av = DB::table('rifcliente_filtro')
->join('clientes_incid', 'rifcliente_filtro.rif', '=', 'clientes_incid.rif', '=', 'incidencias.rif')
->where('clientes_incid.cat_cliente',…

Laravel Oscar
- 35
- 7