Coder Social home page Coder Social logo

Comments (22)

staudenmeir avatar staudenmeir commented on June 14, 2024 2

Check out the latest release, it should fix your issue.

from eloquent-json-relations.

staudenmeir avatar staudenmeir commented on June 14, 2024 1

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.

staudenmeir avatar staudenmeir commented on June 14, 2024

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.

greaterking avatar greaterking commented on June 14, 2024
//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.

staudenmeir avatar staudenmeir commented on June 14, 2024

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.

greaterking avatar greaterking commented on June 14, 2024

....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.

staudenmeir avatar staudenmeir commented on June 14, 2024

You need the accessor or a JSON cast.

from eloquent-json-relations.

greaterking avatar greaterking commented on June 14, 2024

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.

greaterking avatar greaterking commented on June 14, 2024

ok I got it working it was an issue with my table seeders thanks!

from eloquent-json-relations.

greaterking avatar greaterking commented on June 14, 2024

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.

staudenmeir avatar staudenmeir commented on June 14, 2024

What's the result of \App\User::find(1)->getAttributes()['options'] after sync()?

from eloquent-json-relations.

greaterking avatar greaterking commented on June 14, 2024

[{"value": 'xyz', "role_id": 3}]

from eloquent-json-relations.

staudenmeir avatar staudenmeir commented on June 14, 2024

But \App\User::find(1)->roles works?

from eloquent-json-relations.

greaterking avatar greaterking commented on June 14, 2024

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.

greaterking avatar greaterking commented on June 14, 2024

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.

staudenmeir avatar staudenmeir commented on June 14, 2024

Is it actually 'xyz' (with single quotes)? Or "xyz"?

Are you using MySQL?

from eloquent-json-relations.

greaterking avatar greaterking commented on June 14, 2024

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.

staudenmeir avatar staudenmeir commented on June 14, 2024

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.

greaterking avatar greaterking commented on June 14, 2024

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.

staudenmeir avatar staudenmeir commented on June 14, 2024

Thanks, I found the issue. It's a bigger one...

from eloquent-json-relations.

greaterking avatar greaterking commented on June 14, 2024

Ah ok thanks for looking into it...let me know how I can help.

from eloquent-json-relations.

greaterking avatar greaterking commented on June 14, 2024

Working perfectly now! I'll let you know if I find anything else.

from eloquent-json-relations.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.