staudenmeir/eloquent-json-relations

Need a basic Understanding for a json_encoded column

abhinay1435 opened this issue · 6 comments

Hello I have a Model named GridMapping

which has a following column "broker_ids" which is a json column

in this broker_ids columns i save the data as json_encode($oRequest->get('broker_ids'))

The Data Gets saved as the following
[
"2","3","8"
]

now What i need to achieve is when I get all GridMapping
GridMapping::with(['brokers'])->get();

it should bring eager loading of broker that are present in the broker_ids array column

is it possible to achieve this via this package ?

Hi @abhinay1435,

Yes, you can define a BelongsToJson relationship:

class GridMapping extends Model
{
    use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;

    protected $casts = [
       'broker_ids' => 'json',
    ];
    
    public function brokers()
    {
        return $this->belongsToJson(Broker::class, 'broker_ids');
    }
}

class Broker extends Model
{
    use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
}

in this broker_ids columns i save the data as json_encode($oRequest->get('broker_ids'))
The Data Gets saved as the following
[
"2","3","8"
]

You need to convert these IDs to integers because their type has to match the type of the brokers.id column.

Hello @staudenmeir

Thanks for your Quick Reply.
I am still not able to achieve it.

Here is more info

GridMapping.php Code

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Auth;
use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;

class GridMapping extends Model
{
    use HasFactory, SoftDeletes;

    const PENDING = 1, ACTIVE = 2, INACTIVE = 3;
    const PAYOUT_ON_OD_PREMIUM = 1, PAYOUT_ON_NET_PREMIUM = 2;

    protected $casts = [
        'broker_ids' => 'json',
     ];
     
    public function brokers()
    {
        return $this->belongsToJson(Broker::class, 'broker_ids');
    }
}

Broker.php Code

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Auth;
use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;

class Broker extends Model
{
    use HasFactory, SoftDeletes;

    const PENDING = 1, ACTIVE = 2, INACTIVE =3;

    protected $fillable = [
        'name',
        'status_id'
    ];

    public function scopeActive($query)
    {
        return $query->where('brokers.status_id',self::ACTIVE);
    }

    public function grid_mappings()
    {
       return $this->hasManyJson(GridMapping::class, 'broker_ids');
    }
}

Database Snapshot
Screenshot 2023-12-03 at 1 31 40 PM

GridMappingController.php Code

$oGridMappings = new GridMapping();

        $oGridMappings = $oGridMappings->with(['brokers'])->get();
       dd($oGridMappings);

The Response that I get is as below

Screenshot 2023-12-03 at 1 33 35 PM

Please Correct which Part Am i not doing correctly.

There must be a typo.

$oGridMappings = $oGridMappings->with(['brokers'])->get();

Is this your literal code?

What line of code does the exception come from?

Hello @staudenmeir

I am once again attaching the actual snapshots
GridMapping.php (Model)
Screenshot 2023-12-06 at 1 04 29 PM

GridMappingController.php
Screenshot 2023-12-06 at 1 06 18 PM

Broker.php (Model)
Screenshot 2023-12-06 at 1 06 48 PM

Database
Screenshot 2023-12-06 at 1 08 37 PM

Grid Mapping Model migration
Screenshot 2023-12-06 at 1 09 08 PM

Error
Screenshot 2023-12-06 at 1 07 12 PM

There is no typo Error
There is something which I am doing wrong.

Can you try to do the same on your side

What did you change since your last comment? The error messages are different: [broker] vs. [brokers]

Are you using the HasJsonRelationships trait inside your models? The screenshots only show the import outside of the model.

288327387-6f952569-bf7a-4072-84c6-4d0e1df3e8ab

Hello @staudenmeir

It finally worked
I used the

use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;

inside the Model and it worked
Screenshot 2023-12-06 at 11 05 58 PM

Thanks a lot @staudenmeir