Comments (22)
Check out the latest release, it should fix your issue.
from eloquent-json-relations.
This code works for me. When I remove $casts
, I get the "PHP Warning: array_key_exists() expects parameter 2 to be array" error you mentioned earlier.
from eloquent-json-relations.
How is the roles
relationship defined?
Are you using the latest release v1.1.1
?
Are you using the HasJsonRelationships
trait in both the User
and the Role
model?
from eloquent-json-relations.
//User.php
public function roles()
{
return $this->belongsToJson('App\Role', 'options[]->role_id');
}
//Role.php
public function users()
{
return $this->hasManyJson('App\User', 'options[]->role_id');
}
Using v1.1.1 and yes HasJsonRelationships is in User and Role models
...in fact when I do \App\Role::find(2)->users I get
PHP Warning: array_key_exists() expects parameter 2 to be array, null given in P/vendor/laravel/framework/src/Illuminate/Support/Arr.php on line 151
The data is stored in Users table like this: [{role_id: 1, other_field: 'xyz'},{role_id: 2, other_field: 'xyz'}]
schema is basically the same as in example:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->json('options');
$role_id = DB::connection()->getQueryGrammar()->wrap('options->role_id');
$table->unsignedInteger('role_id')->storedAs($role_id);
$table->foreign('role_id')->references('id')->on('roles');
});
``
srry not sure what I'm doing wrong...thanks again.
from eloquent-json-relations.
You can't access the relationship in getOptionsAttribute()
because the result of this accessor is required for the relationship to work.
from eloquent-json-relations.
....ah yes you're right Thanks!
But shouldn't \App\Role::find(2)->users work as well? I just get PHP Warning: array_key_exists() expects parameter 2 to be array, null given in P/vendor/laravel/framework/src/Illuminate/Support/Arr.php on line 151
I'm no longer doing anything in the accessor and have removed it. When doing \App\Role::find(2)->users in tinker I get the error
Essentially get all users with have role_id 2
from eloquent-json-relations.
You need the accessor or a JSON cast.
from eloquent-json-relations.
Just so I understand what do you mean by casting .. in Users model I'm trying to get the roles relationship. Per the example it only shows that options
needs to be casted to json in the User
model. So then in the Role
model to get users what must be done other than
public function users()
{
return $this->hasManyJson('App\User', 'options[]->role_id');
}
I've used the exact example in the read me and wondering if I've missed something maybe my code looks like the readme exaclty because I'm testing before implementing further
class User extends Model
{
use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
protected $casts = [
'options' => 'json',
];
public function roles()
{
return $this->belongsToJson('App\Role', 'options[]->role_id');
}
}
class Role extends Model
{
use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
public function users()
{
return $this->hasManyJson('App\User', 'options[]->role_id');
}
}
from eloquent-json-relations.
ok I got it working it was an issue with my table seeders thanks!
from eloquent-json-relations.
hmm something still seems off...
I detached some things and continue to get the error to reproduce error:
two records in User
User id 1
options: [{'role_id': 1, value: 'abc'}, {'role_id': 2, value: 'def'}, {'role_id': 4, value: 'ghi'}]
User id 2
options: [{'role_id': 4, value: 'abc'}, {'role_id': 5, value: 'def'}, {'role_id': 6, value: 'ghi'}]
\App\Role::find(4)->users
// it returns User 1 & 2 | ok
\App\Role::find(6)->users
// it returns User 2 | ok
\App\Role::find(2)->users
// it returns User 2 | ok
everything ok so far all relationships work...
When I do this:
\App\User::find(1)->roles()->sync([3 => ['value' => 'xyz']])->save() | ok
But now when I do this...
\App\Role::find(3)->users ...results in ... PHP Warning: array_key_exists() expects parameter 2 to be array, null given in /Users/ralphtheart/Sites/focus/vendor/laravel/framework/src/Illuminate/Support/Arr.php on line 151
it should return User 1 but fails.
from eloquent-json-relations.
What's the result of \App\User::find(1)->getAttributes()['options']
after sync()
?
from eloquent-json-relations.
[{"value": 'xyz', "role_id": 3}]
from eloquent-json-relations.
But \App\User::find(1)->roles
works?
from eloquent-json-relations.
hmmm actually no...it returns null
hmm why is that? ... I see the json record in the database in the options column
from eloquent-json-relations.
is it my schema?.. I have this
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->json('options');
$role_id = DB::connection()->getQueryGrammar()->wrap('options->role_id');
$table->unsignedInteger('role_id')->storedAs($role_id);
$table->foreign('role_id')->references('id')->on('roles');
});
from eloquent-json-relations.
Is it actually 'xyz'
(with single quotes)? Or "xyz"
?
Are you using MySQL?
from eloquent-json-relations.
In the db I see double quotes. and yes using mysql
mysql Ver 14.14 Distrib 5.7.24, for osx10.11 (x86_64) using EditLine wrapper and laravel 5.6
from eloquent-json-relations.
What exact version of Laravel are you using?
Do you see single quotes in Laravel? How are you outputting the JSON value? With echo
? Or dd()
?
from eloquent-json-relations.
Laravel Framework 5.6.39
everything id is double quotes the standard relation of the relation works ..User -> Role but not the inverse... Role -> User after syncing or attaching... the output is simply coming from php artisan tinker
from eloquent-json-relations.
Thanks, I found the issue. It's a bigger one...
from eloquent-json-relations.
Ah ok thanks for looking into it...let me know how I can help.
from eloquent-json-relations.
Working perfectly now! I'll let you know if I find anything else.
from eloquent-json-relations.
Related Issues (20)
- pivot always return the same data on belongsToJson HOT 3
- Support for filtering by pivot value? HOT 5
- 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
- I need help with hasManyThroughJson HOT 1
- 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.