Laravel eloquent table relationship for sorting data - php

I am trying to develop an e-commerce website in Laravel but I am having trouble with sorting the data of a category. I tried it through eloquent but unable to sort by price. I tell you my database table structure.
Table category
id | user_id | name | slug | description | timestamp
Table product
id | user_id | name | slug | content | tags | extras | feature_image | timstamp
Table category_product_relation
id | category_id | product_id | timestamp
Table product_price
id | product_id | price | tax | discount | quantity | timestamp
Now, I want to sort products of a category by price. Let me know if this db structure is good or not. If it is not good kindly guide me.

Related

MYSQL better way to create category tables

I am trying create a schema for profile management system.
Since each profile will belong to different category for example sports,political ,non profit organization etc.
Each profile contain different info or details from one another so
1.My question is it better to create all columns in single table or creating different tables for each category because if i create in single category it will be large columns in mysql when category will increases or each category may come some related tables ?
users
id | first_name | middle_name | last_name | dob | primary_email | secondary_email | password | primary_phone | secondary_phone | home_address |
profile_categories
id | category_name |
user_categories
id | user_id | category_id
political
id | category_id | political_person_name | .....

Laravel Eloquent complex query building

I have trouble to achieve my goal to build a filterable complex query.
I have 5 tables. One of them is a time tracking table. To keep that global, I just store the item_id and the time_categories_id to recognize which item it is.
I also need some additional information which is not directly in relation, so for example, I need the client informations which are assigned to a task.
So I have following tables (in short form listed):
times
--------------------------------------------------------
| id | name | description | item_id | time_category_id |
--------------------------------------------------------
time_categories
-------------
| id | name |
-------------
clients
------------------------------------
| id | company| name | description |
------------------------------------
(client has many projects and projects may have many clients)
projects
------------------------------------
| id | company| name | description |
------------------------------------
tasks
----------------------------------------
| id | name | description | project_id |
----------------------------------------
tickets
------------------------------------------------
| id | company| name | description | client_id |
------------------------------------------------
Now I want to create a filter function in which the return should depend on if client or project id is submitted.
For this filter, I created following query:
$timeQuery = Time::select(array(
'times.*',
'time_categories.name as type',
'tasks.name as task_name',
'tasks.description as task_description',
'tickets.name as ticket_name',
'tickets.description as ticket_description'))
->join('time_categories','times.time_category_id','=','time_categories.id')
->leftJoin('tasks',function($join) use ($project){
if(!empty($project))
{
$join->on('times.item_id', '=', 'tasks.id')
->where('tasks.project_id','=',$project);
}else{
$join->on('times.item_id', '=', 'tasks.id');
}
})
->leftJoin('tickets',function($join) use ($client){
if(!empty($client))
{
$join->on('times.item_id','=','tickets.id')
->where('tasks.project_id','=',$client);
}else{
$join->on('times.item_id','=','tickets.id');
}
});
So I ended up here. I don't get an error, but I receive all data instead of filtered data only dependent on client or project id.
Does anyone have an advice?

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

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')

Update tables in database from tables in a different form

I have one working import xml to my mysql, which import to tables.
Now I want to update with these imported tables my new tables. But I don't know how.
The importer import from 3 different xml.
products, price, stock
It's import to products, product_descriptions, images, etc
Example:
Products
ID | Name | stock | date
1 | product 1 | 9999 | 2013.07.13
2 | product 2 | ....
Product_description
ID | description | price | date | sale price
1 | product 1... | 1$ | 2013.07.17 | 0$
2 | Product 2 is blabla.. | 999 $ | 2013... | 10$
I want to update with these items my another tables:
posts
Post ID | post title | description | product ID
100 | products-name | product_description-description | product-ID <- with these item from another tables.
and,
postmeta
post id | meta | value
100 | price | products_description - price
100 | sale price | product_description - sale price
100 | title | products-name
100 | image | imagepath
I don't know how can I do this.
Anyone help me?
You can use INSERT INTO ... SELECT construction.
There is a nice manual from MySQL: http://dev.mysql.com/doc/refman/5.0/en/insert-select.html
For instance,
INSERT INTO posts (post_title, description)
SELECT
Products.Name, Products.stock
FROM Products
This will insert to the posts table all names & stock levels of your products in a title and description columns
P.S. I do not see any connection between Products and Product Description. Are there any foreign keys?

MySQL Select from multiple tables

I'm new to MySQL. I am creating a checkout page in PHP. When the users select the items they want to buy and click "Add to Cart", a temporary table gets created which has the following fields (table name is temp):
+--------------+-----------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------+------+-----+-------------------+----------------+
| Cart_Item_ID | int(11) | NO | PRI | NULL | auto_increment |
| Item_ID | int(11) | NO | | | |
| Added_On | timestamp | YES | | CURRENT_TIMESTAMP | |
+--------------+-----------+------+-----+-------------------+----------------+
I'm only inserting to the Item_ID field which contains the ID of each item they bought (I'm populating the forms with item IDs). What I want to do is look up the item's name and price that's stored in the Inventory table. Here's how that looks:
+--------------+----------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------------+------+-----+-------------------+----------------+
| Inventory_ID | int(11) | NO | PRI | NULL | auto_increment |
| Item_Name | varchar(40) | NO | | | |
| Item_Price | float unsigned | NO | | 0 | |
| Added_On | timestamp | YES | | CURRENT_TIMESTAMP | |
+--------------+----------------+------+-----+-------------------+----------------+
So how would I pull out the Item_name and Item_Price fields from the Inventory table based on the Item_ID field from the temp table so I can display it on the page? I just don't understand how to formulate the query. I'd appreciate any help. Thank you.
It's called JOIN - read more here
SELECT Inventory.Item_Name, Inventory.Item_Price
FROM Inventory, temp WHERE Inventory.Inventory_ID = temp.Item_ID
what i understand is that the Item_ID in temp table is referencing to the Inventory_ID in inventory table. based on this assumption you can use the following query.
Select Item_Name, Item_Price from Inventory, Temp where Temp.Item_ID == Inventory.Inventory_ID
i guess this is what you want to do.
Thanks
As it stands, you can't (unless Inventory_ID = Item_ID)
What you need is a way of JOINing the two tables together. In this instance, if Inventory_ID = Item_ID then the following is possible:
SELECT Item_Name,
Item_Price
FROM InventoryTable
INNER JOIN TempItemTable ON (InventoryTable.Inventory_ID = ItemTable.Item_ID)
If you want to filter for a particular item you can add the constraint:
WHERE ItemTable.Item_ID = 27 --for example
That will join all the rows in your inventory table with matching rows in the Item table.
Jeff Atwood has a great (IMO) visual explanation of how JOINs work.

Categories