Laravel Relation based on value within an JSON object Laravel Relation based on value within an JSON object laravel laravel

Laravel Relation based on value within an JSON object


I created a package with JSON relationships: https://github.com/staudenmeir/eloquent-json-relations

Since the foreign key is in the Prices model, you should use a BelongsTo relationship:

class Prices extends Model {    use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;    protected $casts = ['object' => 'array'];    public function user() {        return $this->belongsTo(User::class, 'object->user_id');    }}class User extends Model  {    use \Staudenmeir\EloquentJsonRelations\HasJsonRelationships;    public function prices() {       return $this->hasMany(Prices::class, 'object->user_id');    }}