Comments (5)
Hi @madsem,
How is landerOfferSets()
defined?
from eloquent-json-relations.
Hey @staudenmeir 👋
it's defined like so:
/**
* Get the default Offer Sets for this Lander.
*
* @return \Staudenmeir\EloquentJsonRelations\Relations\BelongsToJson
*/
public function landerOfferSets(): BelongsToJson
{
return $this->belongsToJson(LanderOfferSet::class, 'lander_offer_sets[]->lander_offer_set_id');
}
from eloquent-json-relations.
Can you log the executed query?
from eloquent-json-relations.
Hey @staudenmeir, thank you!
There seems to be something wrong, I am unsure what is going on.
When I trigger this query by visiting an api endpoint in my browser:
return Lander::query()
->withoutGlobalScopes()
->where('identifier', $this->landing_page_identifier)
->withWhereHas('landerOfferSets', function ($q) {
$q->whereJsonContains('landers.lander_offer_sets', ['lander_element_identifier' => $this->element_identifier]);
})
->first();
Telescope shows this in the query tab:
select
*
from
`landers`
where
`identifier` = '5c0f6264-65e8-3b9a-8aae-33a6f96748ef'
and exists (
select
*
from
`lander_offer_sets`
where
json_contains(
`landers`.`lander_offer_sets`,
json_object('lander_offer_set_id', `lander_offer_sets`.`id`)
)
and json_contains(
`landers`.`lander_offer_sets`,
'{\"lander_element_identifier\":\"23556a9a-0031-332f-86a9-95730a4f8ad1\"}'
)
and `lander_offer_sets`.`deleted_at` is null
)
limit
1
Here's the result when I manually run this in my sql client:
But in my browser, I get an exception message with a totally different query:
https://flareapp.io/share/17xQpR85
PS: My goal is to have the Lander
with loaded relationship for the LanderOfferSet
with id 7
, ie: the one that has a matching lander_element_identifier
in landers.lander_offer_sets
.
But whatever I do, I always get back ALL LanderOfferSet
, or an exception where it tries to query lander_offer_sets
as a table.
from eloquent-json-relations.
I don't see a way to constrain an eager loading query like this.
But in my browser, I get an exception message with a totally different query:
https://flareapp.io/share/17xQpR85
The eager loading query fails because you are referencing the landers
table which isn't available here (only in the whereHas()
subquery).
I only see a manual workaround like this:
$lander = Lander::query()->...->first();
$lander->setRelation(
'landerOfferSets',
$lander->landerOfferSets->where('pivot.lander_element_identifier' => $this->element_identifier)
);
For a query constraint, my only idea is something horrendous like this:
$landerOfferSets = $lander->landerOfferSets()->whereHas(
'landers', // Reverse "HasManyJson" relationship
function($query) use($lander) {
$query->where('id', $lander->id)
->whereRaw("json_contains(`lander_element_identifier`, json_object('id', `lander_offer_sets`.`id`, 'lander_element_identifier', ?))", [$this->element_identifier]);
}
)->get();
from eloquent-json-relations.
Related Issues (20)
- pivot always return the same data on belongsToJson HOT 3
- Support for custom key HOT 1
- BelongsToJson returns wrong results HOT 2
- hasMany - belongsToJson - hasManyJson HOT 4
- Artisan model:show failed HOT 1
- Need a basic Understanding for a json_encoded column HOT 6
- getAttribute() conflict HOT 1
- SqliteGrammar / Sqlite support HOT 11
- laravel compoships HOT 4
- Defining Json Relation in factory definition HOT 3
- Not an issue, really need a help HOT 2
- Nested Array of Objects throws array_key_exists(): Argument #2 ($array) must be of type array, null given HOT 2
- has and WhereHas doesn't work HOT 1
- One dimensional array HOT 5
- MySQL MEMBER OF () performance improvements HOT 5
- Is it possible to define a relationship using a key that can be nested at variable depths? HOT 14
- Great Package HOT 1
- String values fail for belongsToJson and HasManyJson HOT 4
- Many To Many Eager Loading Breaks with array_key_exists(): Argument #2 ($array) must be of type array, null given HOT 5
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-json-relations.