Laravel: how to make a key of a column name? - php

I'm bending my mind for some time now over this problem. Could someone please help me?
I have two tables: products and product_attributes. The product has all basic product information and product_attributes has all specific information for products on different categories. It's much like the magenta attribute system. this table has 4 columns: id, product_id, attribute_name, attribute_value.
Now let's say a product has 2 attributes:
------------------------------------------------------
| id | product_id | attribute_name | attribute_value |
------------------------------------------------------
| 1 | 123 | length | 123cm |
------------------------------------------------------
| 2 | 123 | material | Denim |
------------------------------------------------------
| 3 | 123 | season | Summer |
------------------------------------------------------
Now if I set up the eloquent relationships and query a product, I get a product object with all three attributes. So far this is what I wanted. But now in a blade template I would like to be able to do something like this:
$product->attribute->length
Is this even possible or do I need to achieve these kind of things with a total different approach (like creating different tables for different product types/categories)?
Thanks in advance!

length is a tuple value not an attribute you need
$product->attribute->where('attribute_name', 'length')
or
$product->attribute->whereAttributeName('length')

Related

multiple fields in mysql

Ok, I hope I can make this question as clear as possible.
I have the following table:
table: phones
phone_id | name | ... ... ... | manufacturer_code
-----------------------------------------------------
1 | samsung | | 001
2 | apple | | 002
3 | htc | | 003
and so on...
I have A LOT of columns (... ... ... means at least 40 different columns like width, height, depth, color, has_bluetooth, ... stuff like that).
Now I want to do this differently, but I have NO IDEA how to start.
How I THINK to do it? (correct me if i'm wrong!)
1) Store the field names in a different table
table: phone_fields
field_id | name
--------------------------------
1 | name
2 | width
3 | weight
4 | depth
... | ...
41 | manufacturer_code
2) Connect the fields with the data for each phone in another table
table phones
row_id | phone_id | field_id | value
------------------------------------------------------
1 | 1 | 1 | samsung
2 | 1 | 2 | 10,50 cm
3 | 1 | 3 | 1 kg
... (and so on).
I want this to be searchable/filterable, I have a filter now which filters on brand, color, pricerange, ... and this works fine.
How should you guys do it? Or does anyone have a useful link/tutorial about this? English is not my native language so I don't know exactly how to search for it.
Thanks in advance!
Edit: why am I doing this? I want to be able to add extra fields through my admin-panel if necessary (and not via phpmyadmin or something like that).
If your request always returns the entire set of fields, then leave it in one table
If not, then you can leave only the most important fields in one table, and in the other table place the remaining attributes and link it via foreign key

Relational database with multiple preferences and categories

I'm trying to wrap my head around designing my database which will store one or more preferences for many categories for each user. So in other words, each user can select one or more options from the Colors category, one or more options from the Shapes category, and so on.
My initial thought was to first have a User table with generic user information. Next, there would be a table to store all the different categories as so:
CATEGORY_ID | CATEGORY_VALUE
--------------------------------
1 | Colors
2 | Shapes
3 | Sizes
I'd separate each Category into it's own table (Colors for example):
OPTION_ID | OPTION_VALUE
------------------------------
1 | Red
2 | Blue
3 | Green
Finally, I would have a User Preferences table:
USER_ID | CATEGORY_ID | OPTION_ID
----------------------------------------
1 | 1 | 2
1 | 1 | 3
1 | 3 | 2
2 | 1 | 3
Am I on the right track here or is there a better/more efficient way to designing this. I will be setting up a search results page which will allow visitors to filter through these different categories.
Thanks!

how to implement same product of different weights

I don't know if this question should be here or not but i don't see any other place fit to ask my problem.
See, i am developing an online food portal which obviously sells food , snacks ,cakes and desserts.The real deal comes up when i have to sell a cake which can exist in various weights such as 0.1|0.5|0.9|...4.5 and every one of them costs different.Furthermore, every one of them are present in different quantities in warehouse.
My solution to achieve this was to provide a different row for every variant
ID | product_code | product name | Quantity | price | weight | company_id
1 | 12345 | beer cake | 34 |345 |0.5 |343434defee
2 | 12345 | beer cake | 343 |600 |1.0 |343434defee
3 | 12345 | beer cake | 4 |845 |1.5 |343434defee
4 | 12346 | vodka cake | 341 |345 |0.5 |343434deereee
Here , i looked every product to be a different product,I provided the relation using product code while to differentitate every row , i choose id,product_code as the primary key.
But, using this method involves redudancy as well, every row is looked as a different product.So if i were to display them using php it would show them as different product.
Something like this,
How do i change my structure of my database in order to achieve a single product but with different variants option on the same item?
I could use a php solution , if it's there.
Regards,
BOTjr.
Split the table into two with product_code as foreign key.
Table One
ID | product_code | product name | company_id
1 | 12345 | beer cake |343434defee
2 | 12346 | vodka cake |343434deereee
Table Two
Product_code |Quantity | price | weight
12345 | 34 |345 |0.5
12345 | 343 |600 |1.0
12345 | 4 |845 |1.5
12346 | 341 |345 |0.5
Use Table One to display items, Use Table Two is product description page to select the right quantity

Doctrine multiple 1 to 1 connections

I have task that would be quite simple using regular SQL query but the project is built using doctrine and I am looking for an optimal solution. Maybe someone could advise what would be a good way to approach this.
I have a quite complicated db structure but the simplified version of objects in question look like this:
| Category | | Product | | ProductOption |
------------ --------------- --------------------
| id | | id | | id |
| name | | category_id | | product_id |
------------ | name | | some_data |
--------------- --------------------
Product Option and Product have 1 to 1 connection. But options are created per category (I get 1 entity per category, but need to replicate that entity for every product and store that as 1 to 1 since at some point those options will need to be edited individually. Now there are many ways to do that (the dirty way) , but I would like some advice on how to do that in the most optimal way.

How to create a grid from three mysql tables

I am trying to create a grid with yii using three different tables. Here is a rough demonstration of what I am trying to achieve:
Table: Products
prod_id, prod_name
Table: Regions
reg_id, reg_name
Table: Prices
price_id, prod_id, reg_id, price
Products/Regions | Region#1 | Region#2 | ... | Region#N
--------------------------------------------------------
Product#1 | Price#1 | Price#2 | ... | Price#N
Product#2 | Price#1 | Price#2 | ... | Price#N
Product#3 | Price#1 | Price#2 | ... | Price#N
...
Product#N | Price#1 | Price#2 | ... | Price#N
I need to approach this from the products table. I need to be able to search the products. And I don't need to to use GridView, also it would be optimal if I can use it.
I really need recommendation more than anything in this situation. One idea I have is to create a double array of prices and access them and put them in the table $prices[$prod_id][$reg_id] but this system has to be really optimal and performance plays a huge role. So, I would appreciate if anyone can help me out with this.
Thanks,
Gasim

Categories