Relation tables n:n on laravel - php

I'm new to Laravel and I have a doubt, I have two tables n: n and I want to create an additional table that stores the two ids of each table. Such as shop and product.
How do I create a store by creating an product automatically in the table shop_product?

save shop data
$shop->save();
$shopid= $shop->id;
after save fetch data from shop and simillarly from product
$product->save();
$productid= $product->id;
finally use both variables in shop_product like this
$shop_product->shop_id= $shopid;
$shop_product->product_id= $productid;

Related

Count other table data and display in primary table in php laravel with select subquery

I have two tables as category and product. I have to count similar data from table product and display count in table category. I am using this query in laravel to display category detail.
$data = CategoryModel::orderBy('id','ASC')->get();
In core php I used the following query to display counted data in table 1.
SELECT tbl_category.*,(SELECT COUNT(*) FROM tbl_product
WHERE tbl_product.v_category = tbl_category.v_name) AS TOTAL FROM tbl_category
Now I want to implement above query in laravel. how can I do that ?
I need more details but I am assuming that a product is mapped to multiple categories so there would be a model file named Category mapped to table categories and there would be Product.php model file mapped to products table
Below snippet in your controlled would help
App\Product::withCount('categories')->get()
Please sure to have below in your Product.php (Model file)
public function categories()
{
return $this->hasMany('\App\Category');
}
Note the count of the categories associated with a product would be return as categories_count in the collection

magento how to get attributes associated with a single product using database query

We are trying to get all attributes associated with each product using a database.
We have been used following query but I think the table we are using is not the right one.
$sql = "SELECT attribute_id FROM catalog_product_index_eav WHERE entity_id=".$entityprodid;
This table is not the exact table which will get all attributes attached to a single product id.
Any suggestions how do we get attributes of a single product in Magento using database ??
Magento uses flat tables to centralize the attributes of products, so to query all the attributes by the ID, you can use the table catalog_product_flat, remembering that for this table to be updated, it is necessary to reindex the data.

Save history products in Laravel

I want to write a method to save the history of changes in the table. I have three tables (products, articles and categories). When a user makes a change, for example in the product name. I want to display a message at the product, example: User Jack change the name of product with "ball" on the "ball2016."
I came up with that I created new pivot table "history_products" wherein the columns will: "user_id", "products_id", "created_at" and "updated_at". This table will be connected with table products. I want to used this trigger. You think it's a good idea or in a Laravel can do it in an easier way??
a good source/library is revisionable, the following:
VentureCraft/revisionable

Create an activerecord for using more than one table

I want to create an active record in yii2, same as other models based on activerecord but the difference is that i want use more than one table.
For example, I have a table in database called orders and another table order-items
An order item is a product that buy user in order and with order-id is connected to orders table. Now i want a model for controlling both and use active records features.
For example I could get customer id that saved in order table and also an array of product that saved in order-items.
Look code below:
//get name of customer that saved in order table
$customer = $model->customer;
//get an array of products saved in order-item
$items = $model->items;
//get number of item 3 saved in order-item
$num = $items[3]->number;
It is better If you can use the relation in activerecord class.
Refer this link
If you want to have your own activerecord using two table you should rewrite some functions of baseActiveRecord class.
The instruction is like below but you should test and change it in your own case:
Define one table as base table.
Rewrite attributes() method and define some new attribute may exist in table2.
Rewrite find() method or create an new ActiveQuery class to use in find method to create some complex select or join when using find in your searches and select all fields in table 1 and table 2.
Rewrite you save() and update() method of active record to save table 2 fields in database.
Be aware to use transaction in your Rewrited save and update method when saving data in database.

Magento get unique values from custom database table

I created a custom module with a database table with the help of this article.
I have a program which syncs stock from my physical store to online store. Every time I sync the stock, I add the sku,name, sync data and time, also the qty to the custom database table. There can be multiple entries for the same sku in the table as the same product can be synced multiple times.
Initially I was taking a report of the unique entries from the database table using this, where I was only getting the skus.
$collections = Mage::getResourceModel('module/namespace_collection')->distinct(true)->addFieldToSelect('sku')->load();
foreach( $collections as $collection){
//add to csv
}
Now I want to get the sku and qty of the unique entries with the latest sync data and time for that particular sku. To get this I tried to use
$collections = Mage::getResourceModel('module/namespace_collection')->distinct(true)->addFieldToSelect('sku')->addFieldToSelect('qty')->addFieldToSelect('date')->load();
but I am getting all the entries in the custom table. Can some one help me out with this?
Get unique records from collection
$collection = Mage::getResourceModel('module/namespace_collection')
->getCollection()
->distinct(true)
->addAttributeToSelect('sku')
->load();
The above collection if not return desire data, try this
$collection = Mage::getResourceModel('module/namespace_collection')
->getCollection();
$collection->getSelect()->distinct(true);
But this will retrieve distinct values based on id. If you want to retrieve products using distinct sku values, you should group by "sku".
$collection->getSelect()->group('sku');
If you want to debug the query executed :
$collection->getSelect()->__toString();

Categories