Join database table based on Json colum having multiple key - laravel

I am trying to create an ecommerce application using laravel, So when someone orders something, I store data in orders table, in which a column "products" exists.
I store json object in this column.
this is products json:
[{"pid":65,"min_price":"349.0000"},{"pid":71,"min_price":"349.0000"}]
Now, I want to show these order details to the user profile. So, i need to fetch rows from an another table (products), with the primary key same as "pid" in given json object.
please, suggest me how to resolve it.

You can't do this without retrieving it first and looping over it or something.
What you might want to consider is the following:
Remove the column products from the orders table and create a order_product table. This is called a pivot table.
You can create this table by running the command php artisan create:migration CreateOrderProductTable
Now in this table you define 2 foreign keys. order_id and product_id.
If you have done this, you have to create the relations in the Product model and the Order model.
You can now use this relation to query all products that an order has like this: Order::find(1)->products()->get()

Related

How to select specific columns in a polymorphic relationship where table columns are different?

How can I only select specific columns in a polymorphic relationship because the table columns are different?
$query->with(['Postable' =>function($query){
$query->select('business_title','name ','last_name');
}
The business_title column exists in the agency table. Name and last_name exist in the user table. If I select one table other table gets an error of column not found.
Either develop a naming convention that fulfills your needs (ex: columns like 'name', 'title' can be found on many tables), or do not use select in your query and get all coulmns: $query->with(['Postable']); and you would need appropriate ways to read your query results. Or simply use multiple queries to read from different tables which seems to be the rational option.

Laravel - Please advise on table name

I am in trouble with the name of the pivot table.
products ... Product master table
shop ... Shop master table.
shop_sales ... A table that links products and dates. It leads to another table. The column is date and shop_id.
I want to add a table to shop_sales to manage products and the number sold.
shop_sale_products vs product_shop_sale
Does laravel recommend product_shop_sale?
thanks.

Laravel how to move data from table to another

Is there any simple way to move data from table to table using the id? I want to get all the info inside the first table using the id, insert it to another table, and then delete it from current one using Laravel.
Assuming both tables have the same schema you can do:
TableB::create(TableA::find($id));

How to delete an entry in a table along with its data in another table?

I have a table cars with attributes: id, name. I also have another table specs with id, car_id, name. This tables are related with one to many relations from the Models. I also have set up the foreign keys in.
I have a controller manageData where i have a function insertCar which i use to insert data and update both tables. I wan to create another function deleteCar from where i can delete the car along with its specs from the other table
Use onDelete() method in migration when defining foreign key:
$table->foreign('car_id')->references('id')->on('cars')->onDelete('cascade');
In this case when you'll delete a car record, related data will be automatically deleted from another table.
https://laravel.com/docs/5.4/migrations#foreign-key-constraints

value of custom product attribute is not storing in database table

I am new in Magento and I am using 1.7.2. I added one product attribute called product type name. It is a drop down field and values are populated from a different custom table called product_type_name. I have done successfully so far. Now I want to store the value of product type name into DB Table while adding a product. For that I have added a column product_attrb_type_name(attribute code) in catalog_product_flat_1 table.
Now while trying to add the product, it is showing the following error
SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name ''
Also while trying to edit a product, the values are not set in the product entry form.
How to fix this issue? Could you please help me?
Thanks in advance.
Magento uses EAV table structure, so once you add a attribute they are stored in eav_attribute table with specific entity type (4 for product).
Now when you load a product object, say
$product = Mage::getModel('catalog/product')->load({{entity_id}}) , where entity_id is your product id,
you can use get and set function on this object to set the values of the particular product instance, i.e.
$product->setProductTypeName('someValue'); in your case a a dropdown ,so
$product->setProductTypeName(attributeId);
$product->save();
and get by
$product->getProductTypeName();
You do not have to insert in catalog_product_flat tables, that is Magento way to normalize data into flat tables.
Hope it helps!

Resources