Questions tagged [laravel-query-builder]

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.

1522 questions
10
votes
2 answers

Laravel - How to join 2 tables from different db connection?

I have an application that use 2 databases. I need to create a query that joins a table from one database with a table form another but I don't know how to do that. So, I have a connection name mysql and phc. I can communicate with both with no…
user3242861
  • 1,839
  • 12
  • 48
  • 93
10
votes
6 answers

How do I flatten laravel recursive relationship collection (tree collections)?

How do I flatten a collection with hierarchy self referenced models, tree collections into a single dimension collection. I have a self referencing model having parents and children. I want the result to return a eloquent collection, not a simple…
aimme
  • 6,385
  • 7
  • 48
  • 65
9
votes
4 answers

How to check if table is already joined in Laravel Query Builder

I created a query. I want to join my table with students table: $query->leftJoin('students', 'learners.student_id', '=', 'students.id'); But I don't know my table joined before or not. How should I do that?
Roham Rafii
  • 2,929
  • 7
  • 35
  • 49
9
votes
3 answers

reverse() on querybuilder get() changes the collection results

I need to get last 10 records of a table ordered by a data, and reverse them. This is the code before the reverse: $eventi = \App\Model::with('relation_1', 'relation_2') ->orderBy('data_ora', 'desc') ->take(10) ->get(); If I log the…
Giacomo M
  • 4,450
  • 7
  • 28
  • 57
9
votes
1 answer

Laravel query builder where and or where

I want to build a query something like this: select * from users where id=1 AND address="USA" AND (status="active" OR status="pending") how can i do that with laravel query builder? please help. Thanks!
Codeblooded Saiyan
  • 1,457
  • 4
  • 28
  • 54
9
votes
1 answer

Laravel - Search relation including null in whereHas

I'm trying to find a way to search the relationship. So, I have one to one relation (User and UserDetails, where some of the user can be without details). So, the question is... how can I search Users by their details, but also Users without details…
9
votes
2 answers

Laravel - query builder - left join polymorphic relationship, distinct only

So I'm using Vue 2.0 and Laravel 5.3 to create an application. I've implemented my own sortable table with Vue, using the built-in pagination provided by Laravel. Everything's working perfect - except, I'm trying to left join a "media" polymorphic…
Kingsley
  • 977
  • 2
  • 11
  • 27
9
votes
4 answers

Laravel Query builder and table names

I noticed that I write the database table names quite a lot, and in different files, when I use the Query Builder. If I were to change the database table names, I would have to search and change quite many rows in my project. Is this an issue your…
Olof84
  • 919
  • 4
  • 14
  • 29
9
votes
1 answer

Cast in query builder laravel

I want to cast orderBy in query builder laravel because my price is varchar type.. so when it's sorting...the result is far from I want... my script like this DB::table('test')->where(...)->orderBy('price')->get(); i already try something like this…
Surya Matadewa
  • 1,017
  • 5
  • 19
  • 38
9
votes
1 answer

Laravel Query Builder - Where date is now using carbon

How to get only date on database column, I have difficulties using Carbon on controller: $data['nowUser'] = User::where('date', Carbon::today()->toDateString())->get(); Date column looks like this in the database:
Reint
  • 155
  • 1
  • 4
  • 11
8
votes
4 answers

Laravel boolean returns "1"/"0" instead of true/false in query

I have a query that returns a boolean field (by the way, I'm on an SQL Server) on my REST API developed with Laravel and a few days ago it was returning the value as true/false, but now it returns that boolean value as String: "1"/"0". Any idea on…
guillefix
  • 934
  • 1
  • 13
  • 30
8
votes
2 answers

Laravel 5 update all Pivot entries

I have a Many-to-Many-Relationship between the User and the Customview Model: use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable { /** * Customview relation * @return…
Daniel Leicht
  • 83
  • 1
  • 6
8
votes
1 answer

Creating database view in migration laravel 5.2

I'm trying to create a database view via a migration in Laravel 5.2 as I need to hand a fairly complex query to a view. I have models / tables for leagues, teams, players, and points. Each one has a hasMany / belongsTo relationship with the one…
8
votes
3 answers

Laravel 4 query builder - with complicated left joins

I'm new to Laravel 4. I have this query: SELECT a.id, active, name, email, img_location, IFNULL(b.Total, 0) AS LeadTotal, IFNULL(c.Total, 0) AS InventoryTotal FROM users AS a LEFT JOIN ( SELECT user_id, count(*) as Total FROM lead_user …
justin
  • 1,659
  • 5
  • 21
  • 25
7
votes
1 answer

How to make dynamic query in laravel 5.3?

What exactly I wanna do is, I want to make dynamic query in laravel 5.3 based on requested parameters , so in request i will get column names then filters for that query and I don't know tables from which I want to process the data. So, my question…
SaMeEr
  • 361
  • 2
  • 7
  • 22