Comments (12)
Remove the parts of the query you dont need, like permissions and roles
from eloquent-array-query-builder.
I need the roles, but also in this case I have
$exampleArrayQuery = [
'fields' => ['id', 'first_name'],
'include' => [
'state' => [
'where' => [
'name' => '2222',
],
'fields' => ['id', 'name'],
'order' => 'name DESC',
],
]
];
I want the users with their state name, but returns all the data, the where in the "include" is not working, I am using laravel 5.5 can it be the issue?
from eloquent-array-query-builder.
I tried with laravel 5.2 and the same error,
$exampleArrayQuery = [
'fields' => ['id', 'first_name'],
'include' => [ // relations, can have where, order and fields
'state' => [
'where' => [
'name' => 'adasd',
],
'fields' => ['name']
],
]
];
$query = User::query();
$query = $arrayBuilder->apply($query, $exampleArrayQuery);
dd($query->toSql());
is returning "select id
, first_name
from users
where users
.deleted_at
is null" but the relation is not being applied
from eloquent-array-query-builder.
The include
key translates to with()
method of the Laravel Query Builder, if you want to filter out users based on its state do it like this:
$exampleArrayQuery = [
'where' => [
'state.name' => 'adasd',
],
];
which translates to a whereHas()
from eloquent-array-query-builder.
$exampleArrayQuery = [
'fields' => ['id', 'first_name'],
'include' => [ // relations, can have where, order and fields
'state' => [
'where' => [
'state.name' => 'adasd',
],
'fields' => ['name']
],
]
];
Call to undefined method Illuminate\Database\Query\Builder::state()
User model
public function state()
{
return $this->belongsTo(State::class);
}
State Model
public function users(){
return $this->hasMany(User::class);
}
from eloquent-array-query-builder.
The where goes on the base:
$exampleArrayQuery = [
'fields' => ['id', 'first_name'],
'where' => [
'state.name' => 'adasd',
],
'include' => [
'state' => [
'fields' => ['name']
],
]
];
from eloquent-array-query-builder.
ok thank you, now the filter is working with this
$exampleArrayQuery = [
'where' => [
'states.name' => 'California',
],
'fields' => ['id', 'first_name'],
'include' => [
'states' => [
'fields' => ['id','name']
],
]
];
It display the users only in "California" but does not have the state data, it shows
id=>null,
first_name=>'John'
states=>null
the query that returns is:
select id
, first_name
from users
where exists (select * from states
where users
.state_id
= states
.id
and name
= 'California')
and users
.deleted_at
is null
from eloquent-array-query-builder.
Do you have a working project example? I was not able to let it work :(
from eloquent-array-query-builder.
I use it on many private projects of mine but I dont have any public.
I dont know what your problem may be, could you try writing the same query using Laravel's query builder and see if it works as you want?
from eloquent-array-query-builder.
Ok I did other test, I have users that have many posts
$exampleArrayQuery = [
'where' => [
'name' => 'GreatAdmin'
],
'include' => [
'posts' => [
'fields' => ['id', 'title']
],
]
];
$query = User::query();
$query = $arrayBuilder->apply($query, $exampleArrayQuery);
should be the same as:
User::with('posts')->where('name', '=', 'GreatAdmin')->get();
Right?, but the second one returns the user "GreatAdmin" with his posts, but the first one only the user info and relations empty :/
relations: array:1 [▼
"posts" => Collection {#483 ▼
#items: []
}
]
from eloquent-array-query-builder.
Now I see the error, there is a conflict with ->select and ->with based on some posts like this
https://stackoverflow.com/questions/40272411/eloquent-select-method-not-working-with-using-with-method
I let it work removing the "fields" on the relation, event the where in the relation is working now
$exampleArrayQuery = [
'where' => [
'name' => 'GreatAdmin'
],
'include' => [
'posts' => [
'where' => [
'title' => 'test',
]
],
]
];
It returned the user with all posts data, the problem will be if I only want some fields of that posts
from eloquent-array-query-builder.
Oh, good to know, I never ran into this problem cause I rarely use fields
As the Stackoverflow answers says, a workaround like this should work (note the state_id
)
$exampleArrayQuery = [
'fields' => ['state_id', 'id', 'first_name'],
'where' => [
'state.name' => 'adasd',
],
'include' => [
'state' => [
'fields' => ['name']
],
]
];
from eloquent-array-query-builder.
Related Issues (12)
- How to and two query with like? HOT 1
- Installation failed, reverting ./composer.json to its original content HOT 5
- how to send query as JSON instead of query string HOT 3
- Support Laravel 5.8 HOT 3
- Support Laravel 6.0 and 7.0 HOT 2
- Idea: Build query array from existing Eloquent QueryBuilder
- How to AND , OR two condition to each other HOT 2
- groupBy in array query builder HOT 3
- Is it possible to add SUM, MAX, MIN, AVG, COUNT to query builder HOT 6
- Support Laravel 5.3 HOT 4
- Support Laravel 5.4 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eloquent-array-query-builder.