I am a bit confused with Laravel relationships and need some help.
I have a table called: coffees
In the table I have a boolean field called: blend.
If it is a blend, it is made out of a few different coffees percentages.
For example, I have these coffees:
1 colombia
2 costa rica
3 brazil
I have a blend: Espresso which is made out of:
colombia 25%
costa rica 25%
brazil 50%
I created a pivot table called: blend_coffee with these fields:
blend_id
coffee_id
percent
I created this relationship in the Coffee model:
public function blend()
{
return $this->hasMany(Coffee::class, 'blend_coffee', 'blend_id', 'coffee_id')->withPivot('percent');
}
But now, I don't know how to display the coffees and their percentage for each blend.
If I try:
dd($coffee->blend)
I get the id of the blend
If I try:
foreach($coffee->blend as $blend)
I get the error: Invalid argument supplied for foreach()
I hope you can see my confusion and help.
You're trying to do $this->hasMany(...) but this function only only accepts 3 arguments and you're passing 4. You should use hasManyThrough instead
This is how I solved my issue:
I have the coffees table.
I have a pivot table called: blend_coffee with fields:
blend_id, coffee_id, percent
And in the Coffee model I have these relationships:
public function blendCoffees()
{
return $this->belongsToMany(Coffee::class, 'blend_coffee', 'blend_id', 'coffee_id')->withPivot('percent');
}
All is good in my kingdom. :)
Related
I'd like to answer a simple question: What is the avarage age of persons that own cars of specific colors ? ( Using Penatho BI, Yellowfin BI, MariaDB)
To do that the following model was designed.
One fact table "person_fact" containing the columns:
id, name and age.
And a dimension table "auto_dim" containing the columns:
id ( foreign key to person_fact table), car_type, color and hp.
As we see one person can own more than one car!
The problem is that in some filter scenarios, the persons that own several cars are counted multiple times, where they shouldn't.
For instance, trying to calcualte the avarage age of persons that own a "Black" or a "Red" car leads to a wrong result !!!
The SQL query that is generated by Pentaho BI is the following:
select
avg(`person_fact`.`age`) as `m0`
from `auto_dim` as `auto_dim`,
`person_fact` as `person_fact`
where `person_fact`.`id` = `auto_dim`.`id`
and `auto_dim`.`color` in ('Black', 'Red');
The same problem occurs using Yellowfin BI. The SQL query generated by Yellowfin BI:
SELECT DISTINCT AVG(`PERSON_FACT`.`age`)
FROM `person_fact` AS `PERSON_FACT`
INNER JOIN `auto_dim` AS `AUTO_DIM`
ON (`PERSON_FACT`.`id` = `AUTO_DIM`.`id`)
WHERE (`AUTO_DIM`.`color` IN ('Black', 'Red'))
The correct answer is 28.25 !
NOTE: Calculating the avg age with excel yields the right answer using a DAX expression!!
Question:
Is there any possibility to use something like DAX expression for Pentaho BI/Yellowfin BI to get right avg ?
Should i use a another model to fix the problem ?
Thanks in advance !!
I want to show the results of a MySQL query on my website using angularjs. For now, I'm showing them using a simple table with ng-repeat and it works with no problem. But because the data is a lot, I wanted to ask if it is possible to create multiple panels or tables per specific field.
To be more specific, I have 4 fields returned from the query: name, address, occupation, department. Right now I have a table such as:
George Smith Nikis 10 Project Manager Finance
Maria Bexley Lincoln 20 Project Manager Research
Chris Liggs Forks 123 Programmer Computer Science
etc. I want to know if I can create as many panels or tables as the unique values of the "occupation" field are and then show the results per that unique value inside each panel/table. So instead of the above table I would have something like:
Project Manager
George Smith Nikis 10 Finance
Maria Bexley Lincoln 20 Research
Programmer
Chris Liggs Forks 123 Computer Science
I think you need to use groupby filter
Check this fiddle by Darryl Rubarth, it contains the answer you need
http://jsfiddle.net/drubarth/R8YZh/
<div ng-repeat="item in MyList | orderBy:'groupfield' | groupBy:'groupfield'" >
You can use group by filter in ng-repeat with which you need to group
I have a following employee table value as below :
name | cost
john | 1000
john | -1000
john | 5000
when we add the cost column total will be 5000.
I need to print only the 3rd row in BIRT report, since the 1st and 2nd row get cancelled out each other.
I'm stuck at filtering the table for above scenario.
Couldn't you just solve this using SQL like
select name, sum(cost)
from employee
group by name
order by name
?
Or do you just want to exclude two rows if they have exactly the same cost, but with different signs? Note that this is actually something different, take for example the three rows [ john|1, john|2, john|-3 ]? In this case, a pure SQL solution can be achieved using the SQL analytic functions (at least if you are using Oracle).
Elaborate your question. Its not clear if these are columns or rows.
If These are columns:
Create a computed column in your dataset
In Expression builder of that column add/sub values using dataSetRow['col1'] and dataSetRow['col2']
Add only that computed column to your table.
If these are rows
Select rows you don't want to print
Go to properties tab
Find Visibility property and click on it
Check Hide Element option
I have a summary report with a multiples dataset used in differents lookup, I know the lookup compare single values only, I need to compare double value in a datasets and return the value, I'm trying to using a combined source in a first parameter of lookup but I'm not sure if it will be work
dataset1
name team value
----------------
john team1 3
john team2 4
mark team1 2
jane team2 1
I want to use lookup for this dataset using name and team together to build a unique key and get the correct value like a
lookup for "john+team1" and get the value 3
Any help or idea???
Would it not be easier to compare the 2 datasets within SQL rather than trying to do it in SSRS? Temp table1 would be name etc
Write one massive query to combine all of the data, that was it's easier/ quicker to use and it can be used in one table
I am learning cakePHP 1.26.
I got some sample data in a Table Testing which consists of two fields:
(there is not primary key in this sample Table)
Data | Date
Hello | 2010-07-1 15:11:11
World | 2010-07-1 11:01:01
Hi there | 2010-07-02 11:36:11
I need to find out their position number according to the Date when they were written into the Database, i.e. which of them came first and which of them came second.
But I don't know if there is a component in cakePHP that can help me solve the question.
It is easy to list them out according to the Date and number each record manually when there are only a few records in the Database,but when the reocrds grow, it will be difficult for humans to do it this way:
Record 1: Hi there
Record 2: Hello
Record 3: World
In controller
$this->set('results', $this->MyTable->find('all', array('order'=>'date_field')));
In model
foreach($reuslts as $key=>$value){
echo "Record ".$key.": ".$value['MyTable']['name']
}