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');
}
}
GridMappingController.php Code
$oGridMappings = new GridMapping();
$oGridMappings = $oGridMappings->with(['brokers'])->get();
dd($oGridMappings);
The Response that I get is as below
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)
There is no typo Error
There is something which I am doing wrong.
Can you try to do the same on your side
Hello @staudenmeir
It finally worked
I used the
use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;
inside the Model and it worked
Thanks a lot @staudenmeir