I would like to get all products related to a certian color using Eloquent 'hasMany' relationship. However the colors related to products are stored in json value in the products table.
Color Table:
-------------
id | color
-------------
1 Black
2 Brown
3 Gray
Product Table:
------------------------------
id | name | color
------------------------------
1 Shoe Name ["1","2"]
2 Shoe Name ["2","3"]
Color Model
public function products()
{
return $this->hasMany(Product::class, 'color');
}
But unfortunately nothing returns when i do
dd(Color::products());
I know the issue is trying to do a hasMany on a json field, any help or assistance would be great. Unfortunately i won't be able to change the database structure. I need to work with it as is.
Thanks in advance.