Tuesday, March 21, 2017

Laravel/PHP MySQL One to Many Relationship using Json

Firstly is it a good idea to store one to many relationships in the form of a Json column and if so how do I write the query in laravel to retrieve the data

Ex :

I've activities table and verticals table

Activities Table

id | activity | verticals
1  | Dusting  | [1,2]
2  | Oiling   | [3]
3  | Mopping  | [1,2,3]

Verticals Table

id | vertical
1  | House Keeping
2  | Pantry
3  | Engineering & Machinery 

Now in my dashboard showing activities list the result should be

id | activity | verticals
1  | Dusting  | Housekeeping, Pantry
2  | Oiling   | Engineering & Machinery
3  | Mopping  | Housekeeping, Pantry, Engineering & Machinery

Now in the traditional non json method I'd have used laravel Eloquent's self::hasMany But can i do this same if using json

also is it advisable ?



via Sushil Sudhakaran

Advertisement