One To Many Relationship in Eloquent with JSON Column - json

Hi i have "products" table that has json data column called(category_ids):
enter image description here
i need to display all the products in the specific category by id stored in json column but its not working:
enter image description here
enter image description here

Related

SQL read csv formatting in tables

The data I have is formatted in this way:
Apfeltasche;E1412,E1442,E410,E500,E920,E471,E330 \
Apfeltüte;E300,E501\
Balsamico Dressing;E224,E150d,E262,E500,E415\
Barbecue Sauce;E415,E1404\
Big Mac;E471,E472e,E300,E481,E202,E263,E331,E330,E415,E322, E160a,E160c\
Butter;NULL\
Cheeseburger;E471,E472e,E330,E481,E300,E331,E202,E263,E322,E160a,E160c\
The first column is the name of a product and the codes on the right are additives (e-num). I have one table for products that combines the name with an auto-increment id. And I have a table that stores the connection between the product-id and the e-num.
So there are two problems: First I need to load the product-names into the product table to get a valid id for the product. Then I have to combine the ID with the e-nums. But the e-nums are seperated with commas which would have to be a seperate row each time in the table connecting id and e-num. So I would kind of have to do two splits.
Iam really not finding any way to do that. And unfortunatly Iam not allowed to change the csv data to fit my model better. How would you do that ? Thanks guys :)

Linked tables vs json objects

So the question what is better to use a linked tables vs json objects in a mysql database for many to many relationship so for example you have a user table like this
id
name
email
and event table:
id
name
description
The question is if its better to add a extra text field toward the user table with text column where you store a json object where you put event ids in like this
user table:
id
name
email
json
with the json object looking something like this
{
{event_id: 1},
{event_id: 2},
etc
}
or have a linked table with
id
event_id
user_id
assuming you have a many to many relationship where one user can register for multiple events and one event can have multiple users. where you also want to count how much users belong to 1 event and also wanna ask which one is optimal to use for querying and performances while doing this in laravel.
Your linked table has all the information
SELECT COUNT(*) FROM linked_table GROUP BY event_id WHERE event_id = 10
So you now have the number of users that belong to one event.
even with mysql 8 you have to put much more work and code to get information.
Edit:
besides json are slightly better that comma separated field Is storing a delimited list in a database column really that bad?

what is the best schema for this database scenario?

i have a database scenario that there is a product and user and market and all of this have a image and the image have name , alt , description so what is the best structure for the database schema
making only one table for image and reference product and user and market to it
make a images table for each module like Product_images , user_images , Market_images and so on
thanks in advance
Product, User, Market and Image table will be the good choice.
Image id will be mapped to respective table like product, user and market.
All the image attributes should be kept in image table

Using ActiveRecord to achieve complex relations in Rails

From another question of mine:
What I need to achieve is this: Create multiple categories with their
own designated information fields (ie. Cars have different fields from
Pets) and once such a category is created, the commands I need will be
invoked and a new table for each category will be made. I know I could
store all fields as some sort of string and then process it to display
it properly, but I need a advanced search function for my web app and
creating separate tables for each category seems the best way to
achieve it. I would really like to hear alternatives for this
So I have this situation where I need categories to hold all input fields needed for that certain category. So in administration I'd have this form where I'd be able to add the category name, some other relevant information to the category itself, and then these fields that would collect information on what HTML fields to present to the user when making an entry to this certain category.
For example this would be a Dog category:
Category name: Dog
Category enabled: 1
Category parent: Pets
Fields:
Title - text field (will be auto added to each category)
Breed - select field
Age - number field
Color - text field
Price - number field (will be auto added to each category)
Description - text area field (will be auto added to each category)
So now at this stage when the user created all these certain fields for the Dog category, Im having trouble figuring what would happen when the user hits the submit button to save this category. I thought of these two solutions:
Create a new model/table for each new category (Read linked question above) with all the HTML fields as columns and also store a row on the categories table with some basic info about this category
Store everything in the categories table and have a fields_json column which will store all HTML field information (I wont be actually storing HTML, but basic info what the fields are about then create the HTML form fields dynamically in a controller) as a JSON string. I would be able to present the fields nicely on create, but on update it would be a hassle to populate those fields (maybe) and a search function would not be very efficient with this alternative.
So what I'm looking for is a third alternative so I can fix my problem. What would be an efficient way to solve this problem and be able to have categories with different input fields and also be able to efficiently perform searches on these categories?
The project I'm working on is being created in Ruby on Rails 4 and the database is in MySQL.
For given scenario I would:
create table categories to store each category
create table category_fields to store each category field
create table collected_categories to store all collected data from category fields in serialized hash
Collected data can be easily (de)serialized into text column (no matter of db engine you will use).
Check those sources which utilize your problem: dynamic forms

Storing pictures AND a profile picture in MYSQL?

I am trying to make a page where the user has his own photos but also his own profile photo. I am uploading the actual photos to a file server then inserting the photo names into a MYSQL Database. So my table so far is,
photos
-photo_id
-photo_name
-user_id
So how do I declare a photo as a profile photo? What is the common method? Do I create a different table?
Depending on what other photos are in that same table it might make sense to put profile photo filenames in a separate table or to create another column in your table for a boolean photo_type. Read up on designing data structures and normal forms.