Questions tagged [query-builder]

A query-builder is a set of classes and methods that is able to programmatically build queries.

It provides a set of classes and methods that is able to programmatically build queries, and also provides a fluent API.

This functionality can be embedded on an or be independent.

Functionality

  • Queries more readable
  • Can be cross database

Query Builders documentation

2553 questions
153
votes
14 answers

How to use WHERE IN with Doctrine 2

I have the following code which gives me the error: Message: Invalid parameter number: number of bound variables does not match number of tokens Code: public function getCount($ids, $outcome) { if (!is_array($ids)) { $ids =…
Tjorriemorrie
  • 16,818
  • 20
  • 89
  • 131
146
votes
12 answers

How to select from subquery using Laravel Query Builder?

I'd like to get value by the following SQL using Eloquent ORM. - SQL SELECT COUNT(*) FROM (SELECT * FROM abc GROUP BY col1) AS a; Then I considered the following. - Code $sql = Abc::from('abc AS a')->groupBy('col1')->toSql(); $num =…
quenty658
  • 1,587
  • 2
  • 10
  • 7
113
votes
8 answers

Laravel Eloquent vs DB facade: When to use which?

I did some performance tests between Laravel's DB facade query builder and Laravel's Eloquent ORM. The DB facade was much faster than Eloquent for many SQL statements (SELECT, UPDATE, DELETE, INSERT). So why would someone use the slower Laravel…
Panagiotis Koursaris
  • 3,794
  • 4
  • 23
  • 46
87
votes
3 answers

Laravel query builder - re-use query with amended where statement

My application dynamically builds and runs complex queries to generate reports. In some instances I need to get multiple, somewhat arbitrary date ranges, with all other parameters the same. So my code builds the query with a bunch of joins, wheres,…
Geoff Clayton
  • 1,123
  • 2
  • 10
  • 16
85
votes
5 answers

Laravel 5.2 - pluck() method returns array

I'm trying to upgrade my project L5.1 -> L5.2. In upgrade guide there's one thing which isn't clear for me: The lists method on the Collection, query builder and Eloquent query builder objects has been renamed to pluck. The method signature …
Limon Monte
  • 52,539
  • 45
  • 182
  • 213
84
votes
4 answers

How to select distinct query using symfony2 doctrine query builder?

I have this symfony code where it retrieves all the categories related to a blog section on my project: $category = $catrep->createQueryBuilder('cc') ->Where('cc.contenttype = :type') ->setParameter('type', 'blogarticle') …
mickburkejnr
  • 3,652
  • 12
  • 76
  • 109
68
votes
4 answers

Select entries between dates in doctrine 2

I will go insane with this minimal error that I'm not getting fix. I want to select entries between two days, the examples below ilustrate all my fails: opt 1. $qb->where('e.fecha > ' . $monday->format('Y-m-d')); $qb->andWhere('e.fecha < ' .…
manix
  • 14,537
  • 11
  • 70
  • 107
61
votes
9 answers

How does group by works in sequelize?

I am looking for group by queries through Sequelize and cannot seem to find any documentation. SELECT column, count(column) FROM table GROUP BY column
user964287
  • 713
  • 1
  • 7
  • 11
58
votes
6 answers

Laravel Query Builder where max id

How do I accomplish this in Laravel 4.1 Query Builder? select * from orders where id = (select max(`id`) from orders) I tried this, working but can't get the eloquent feature. DB::select(DB::raw('select * from orders where id = (select max(`id`)…
Shiro
  • 7,344
  • 8
  • 46
  • 80
53
votes
8 answers

Group by not working - Laravel

I'm not able to run this simple query in Laravel 5.3 $top_performers = DB::table('pom_votes') ->groupBy('performer_id') ->get(); It gives me: SQLSTATE[42000]: Syntax error or access violation: 1055 'assessment_system.pom_votes.id'…
Parth Vora
  • 4,073
  • 7
  • 36
  • 59
50
votes
9 answers

DB query builder toArray() laravel 4

I'm trying to convert a query to an array with the method toArray() but it doesn't work for the query builder. Any ideas for convert it? Example DB::table('user')->where('name',=,'Jhon')->get()->toArray();
Fabrizio Fenoglio
  • 5,767
  • 14
  • 38
  • 75
46
votes
4 answers

Symfony 2: INNER JOIN on non related table with doctrine query builder

I'm trying to build a query with the doctrine query builder which joins a non related table like this: $query = $this->createQueryBuilder('gpr') ->select('gpr, p') ->innerJoin('TPost', 'p') ->where('gpr.contentId =…
0s1r1s
  • 1,723
  • 1
  • 13
  • 15
43
votes
2 answers

Symfony2 QueryBuilder join ON and WITH difference

I'm new with Symfony2 and I built successfully my first join through QueryBuilder and Doctrine 2. Probably this is a stupid question but both on-line and in the Symfony2's methods I was unable to find anything for understanding the difference…
Roberto Rizzi
  • 1,525
  • 5
  • 26
  • 39
39
votes
3 answers

Query builder not inserting timestamps

I am using Query builder to insert data all fields are been inserted but timestamps like created_at and updated_at are not inserting they all have default 0:0:0 values my insert query is $id = DB::table('widgets') ->insertGetId(array( …
Samundra Khatri
  • 997
  • 1
  • 9
  • 18
39
votes
6 answers

Laravel whereIn OR whereIn

I'm making a products search by filters: My code: ->where(function($query) use($filter) { if(!empty($filter)){ foreach ($filter as $key => $value) { $f = explode(",", $value); $query-> whereIn('products.value',…
Laky
  • 745
  • 2
  • 12
  • 25
1
2 3
99 100