I Have 2 table:
stores
store_feedbacks | store_id , user_id ,rating
rating is number between 1-5 , Now I want to get average of all rating for one store,How I can do it?
Assuming your relationships are correctly set up:
$store->ratings()->avg();
Docs: https://laravel.com/docs/5.7/queries#aggregates - they're from the queries page, but it applies to Eloquent as well.
Related
I have received answers to my last question. Thanks to all. Now I have another query. I have a lookup table, called Products and a transaction table, called Sales. The relationship between them is one to many. I want to make a Calculated Column at Product to get the total number of sales for each product. I tried to apply Calculate function as solution.
Product [total number of sales] =
Countrows ( Filter (Product, Calculate (Sum( Sales [SalesAmount])))).
I expected products in each row in Product table shall filter the sum of sales amount and generate a virtual table with all the entries related to a particular product. And finally, Countrows shall count the number of entries. But on the contrary, it resulted with same number in each row.
I am just a beginner in DAX. I tried to solve it depending on context transition concept. But my guess was wrong. Please help me out. Thank you in advance.
Use RELATEDTABLE:
If you just want the number of entries in the Sales table per product:
=
COUNTROWS(
RELATEDTABLE( Sales )
)
If you want the total sales per product:
=
SUMX(
RELATEDTABLE( Sales ),
Sales[SalesAmount]
)
I have a table 1- Invoices report that looks like this:
Invoice Number
Customer Number
1
cus1
2
cus2
3
cus3
4
cus4
...
...
I want to make a column that has only the Customer Number.
I have used the following Expression:
Invoice Customers Number =
SELECTCOLUMNS(
'1- Invoices report',
"Customer Number",
'1- Invoices report'[Customer Number]
)
With this I am getting the error A table of multiple values was supplied where a single value was expected.
Any solution for this?
The result of SELECTCOLUMNS() is a table, not a column.
Make sure to use it as a calculated table, not a calculated column or measure:
Although code-based answers are much appreciated, but I'm interested in knowing the logic behind handling this, as it might happen again later.
I'm working on an eCommerce project. Most products are variables. Imagine a shirt for example.
A shirt can have 5 different sizes ( XS, S, M, ML, L ) and 3 colors ( Red, Green, Blue ). These are examples, these attributes are dynamic and can be edited any time. My approach was to create 3 different tables for the product itself:
- main_product_table
- id
- product_variant_table
- id
- main_product_id
- product_variant_combination_table
- id
- variant_id
- attribute_name_id
- attribute_value_id
And 2 more tables for the attributes:
- attribute_name_table
- id
- attribute_name
- attribute_value_table
- id
- attribute_value
So, for example the said shirt will now creates the following records:
2 records in attribute_name_table
3 records in attribute_value_table
1 record in main_product_table
3x5 records in the product_variant_table
2x3x5 records in the product_variant_combination_table ( 2 rows each holding the value of 1 attribute )
Now consider querying all products that include a XS size, or query the product with all of its variation data.
My questions are:
Can eloquent models handle this?
If so, should I create 5 models, 1 per table?
If not, should I create a single ( or 2 ) Models ( Product, Attribute ) and the connect them using custom SQLs and methods?
What are the relations here? Attributes seem to have one-to-many to me, but combinations also seem one-to-many and both of these are also connected to each other.
I'm struggling to understand the relations between the above. It seems understandable via SQL, but I can't relate them to each other via Eloquents.
Also, the tables seems to grow massive rapidly. Is this approach proper at all?
Your approach seems very difficult to maintain in the long term. I don't understand why you would have to separate the name attribute from value.
I would create the product model, size and color with its migrations and a pivot table to store the stock
- size_table
-- id
-- name
- color_table
-- id
-- name
- product_table
-- id
-- type (shirt, pants, pullover ... etc.)
-- description
-- status
- stock_table
-- product_id (reference to the product table)
-- color_id (Reference to the color table)
-- size_id (Reference to the size table)
-- quantity (Quantity of products available with these attributes)
-- status
this way you have the same product with more than one combination stored in stock_table.
Now you only need to define the relationships between each model and then you will be able to access all the attributes from product_detail_table
$item->quantity
$item->product->description
$item->size->name
$item->color->name
I have an 'articles' table (Model: Article) where there is field named 'pageview'. Each Article has different number of pageview. I want to fetch those rows who holds top 10 pageviews. How can I fetch these data using eloquent?
You can first orderBy DESC the articles and then take out 10 records of the model. Try this!
$articles = Article::orderBy('pageview', 'DESC')->take(10)->get();
You can do this with the orderBy method which ordering data.
hence you need top 10 page views, you can do this.
orderBy('PageView','desc')
Now you have to take only 10 so use method take(). It actually means limit in MySQL. here's your code
Article::orderBy('PageView','desc')->take(10)->get();
Mysql query is now
select * from articles order by PageView desc limit 10
It will do the trick:
Article::orderBy('PageView','Desc')->take(10)->get();
Article::orderBy('PageView','DESC')->take(10)->get();
or (without column name)
Article::latest()->take(10)->get();
I have a table named contact. In that table, there is a column named phone number.
What I really want to do is get a phone number that exists 2 or 3 times on the table. Is there any way to do that using eloquent?
You could use having count:
$numbers = DB::table('contact')
->select('phone_number')
->groupBy('phone_number')
->havingRaw('COUNT(phone_number) > 1')
->get();