I've 2 Models, let's say: Cart and Product.
Cart has many Product. Product belongs to Cart
3 tables cart, product, cart_product (id, cart_id, product_id)
I insert another field to cart_product, ex: (id, cart_id, product_id, quantity).
When I attach a Product to a Cart how can I query the relation to insert a quantity?
self answer:
$cart->products()->attach($p, array('blablabla', 'blablabla'));
Related
I'm trying to build a farm management app with Laravel and I need help with my database schema. Here are some of my tables
users (id, name)
products (id, trade_name, state, category_id)
expenses (id, category_id)
activities (id, equipment_id)
equipment (id, name, type)
Equipment can either be a tractor or implement. For tractors I want to have the following columns total_distance_traveled and a fuel_quantity
Product can be fertilizers, Insecticide, Herbicides and their state can either be liquid or solid and I want to to keep track of quantity of my products as well (quantity is in liters or kilograms)
I'm not sure if it's a good idea to include the total_distance_traveled and fuel_quantity in the equipment table and a quantity column in the products table
I've thought of creating a new stocks table and have a stockable_id, and stockable_type but that would only work for quantity since it's kind of a common column between products and equipment tables
I'ld like to keep track of total_distance_traveled, fuel_quantity for the equipment table and quantity for products table. e.g. when the total_distance_traveled is changed I'd like to know when that happened so I can have logs of my activities over time
You can create a stock table if you know that this table will only use for product and equipements:
id
stockable_id
stockable_type
quantity
total_distance_traveled // nullable as they have value only when item will be tractor,
fuel_quantity // nullable as they have value only when item will be tractor,
but for same table or if you think there can come other data too then a you can have a json column in which you can save extra attributes for specific item
id
stockable_id
stockable_type
quantity
sepecific_attributes //json where you can save any additional values based on your product
"{'total_distance_traveled':'100','fuel_quantity':20,'some_other_info':'value'}
I have Supplier dimension table has 1:n relationship with InvoiceDetail fact table. I would like to get the list of active suppliers like below SQL, but in DAX language:
SELECT [Id]
,[Name]
,[Code]
,[CountryIso]
FROM [Supplier] s
WHERE EXISTS (SELECT 1 FROM [InvoiceDetail] id WHERE s.id = id.SupplierId)
I am not sure how I can do on Measure with DAX
Assuming that an active supplier means that the supplier has an invoice against them and that your data looks something like this..
Invoice Table
Supplier Table
Creating a relationship between the two tables will in effect, 'join' the two tables.
You can then use invoice number field from the invoice table and the name/code/countryiso from the supplier table.
Example being:
The value are only being drawn from the invoice table, so you'll only see active Invoices.
If being an active supplier means having a true bool value, join the tables and add a report/page wide filter on that bool value.
I have 3 tables
categories
id
name
categories_product
id
category_id
product_id
and a product table
products
id
name
I want products based on catergori_id, how kan i do that with laravel Eloquent?
Simple as this:
Product::whereCategori_id($category)->get();
I received fantastic help from Dave Costa and Justin Cave here (thanks again) in how to calculate a subtotal value from two other attributes, Quantity and Price (so Quantity * Price = Subtotal). In one of the answers it was stated that from a normalisation point of view it's not good to do this as the subtotal value can be derived from the other two attributes and that I should probably look at using a View. I've read up on Views and am getting the concept (I'll be using them elsewhere regardless) but I'm still not sure how to go about actually calculating the two values and show the result in a custom view. If anyone could point me in the right direction I'd appreciate it.
The current trigger (credit to Dave and Justin):
CREATE VIEW show_subtotal
AS SELECT price
FROM products
WHERE product_no =:new.product_no;
:new.subtotal := currentPrice * :new.quantity;
Something like this, for example, will compute the subtotal by joining the order_line and product tables together just as the previous trigger was doing. Presumably, you'd want to include some additional attributes (i.e. the order number, the order line number, etc.)
CREATE OR REPLACE VIEW order_line_with_subtotal( product_no,
quantity,
price,
subtotal )
AS
SELECT ol.product_no,
ol.quantity,
p.price,
(ol.quantity * p.price) subtotal
FROM order_line ol
JOIN product p ON (ol.product_no = p.product_no)
This has many of the same data consistency issues that the trigger-based solution had since the current price is being referenced from the product table rather than the then-current price being stored in the order_line table. If you changed the data model so that the order_line table stored the quantity and the current price, the view would get simpler because it would no longer need to join to the product table
CREATE OR REPLACE VIEW order_line_with_subtotal( product_no,
quantity,
price,
subtotal )
AS
SELECT ol.product_no,
ol.quantity,
ol.price,
(ol.quantity * ol.price) subtotal
FROM order_line ol
If you are on 11g, you can also create a virtual column in your table definition,
CREATE TABLE order_line (
order_line_no NUMBER PRIMARY KEY,
order_no NUMBER REFERENCES order( order_no ),
price NUMBER,
quantity NUMBER,
subtotal NUMBER GENERATED ALWAYS AS (price*quantity) VIRTUAL
);
I have created a custom module and tables for that.
1) How can I use EAV Collection functions on my custom table collection - addExpressionAttributeToSelect, I have to use Count, SUM functions .
for example say I have tableA and tableB
table A has some SKU data, tableB has sales data for SKUs(Multilines for a SKU)
`$collection = Mage::getModel('Module/tableA')->getCollection();`,
Now I need one more column(sum of total sale) in this collection from tableB IF sold date is between two dates.
2) How to write inner queries While preparing collection.