I have a table called jobs and another table called job_questions - a job can have five screening questions. Table job_questions is a type of lookup table but with some additional data as follows:
job_questions
-------------
job_id int
question_id int
question varchar
expected_answer enum ('y', 'n')
job_id | question_id | question | expected_answer
--------------------------------------------------------------
1 | 1 | Do you have experience | Y
There is no table called questions - question_id merely refers to question numbers on a form (1-5) How would you map such a model in laravel since you dont normally have a many to many model class in laravel? Do i create a model class for the lookup table, otherwise I how would i pull the additional information it contains?
If one question can be in multiple jobs, you indeed have a many to many relationship.
You're gonna need one table for jobs and one table for questions to represent the data. To actually make a relationship, you're gonna need a pivot table. Basically it's gonna have a job_id and a question_id, so if job 3 has questions 1, 2 and 3, there will be three rows in this table with the job_id 3 and the three question_id.
You don't need a model for this pivot table, just a model for the two actual data.
In your models, the relationship functions don't change much. Both have the many word: return $this->belongsToMany('App\Job', 'pivot_table_name', 'question_id_column', 'job_id_column'). You also may put this method in the other model so you can query the relationships both ways, question's jobs and job's questions. Parameters 2 and on to the belongsToMany are optional but you'll probably need at least the second to set the pivot table name.
Docs to this is here:
http://laravel.com/docs/5.0/eloquent#many-to-many
and
http://laravel.com/docs/5.0/eloquent#working-with-pivot-tables
Related
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.
There's a couple of tables that I need to use columns from in the select statement. the questions is: Create a View to display the employee id, first name and surname. In your query include the coin price and a 10% commission for the sales made by the employees.
the difficult part for me is that employees of the same employee number, can make multiple coin sales, so in the view, i need to be able to add all the coin sales together of each respective primary key (employee_id)
As you can see in this image, emp101 has sold two different coins, with the coin_id's of "7116" and "7112". In the view i want to be able to somehow tally each coin value that each employee_id has sold if that makes sense ?
There's multiple other tables, but there's too many to send, so i am just trying to get a general idea of how to do this. I understand the logistics of the question, i just dont know how to implement the answer with the correct syntax and methods etc...
Since this appears to be a homework question, here is a discussion of how to solve it:
You want to CREATE a VIEW and give the view a name (something like employee_commisions) and include 5 columns (employee_id, first_name, surname, coin_price and commission - or maybe only 4 columns if they want the combined price plus commission).
To get the values for that view, you want to SELECT ... FROM existing tables; however, your image does not include first_name, surname or a value for a coin so I am assuming that you will have an employees table and a coins table that you will need to INNER JOIN to the invoice table on their respective primary keys.
To get the total value for the coins, you want to aggregate the values and this would be done with the SUM aggregation function and, so that you get the value for each employee, you would need to GROUP BY the employee_id primary key. You will either need to include the other columns you are not aggregating by in the GROUP BY clause or apply an aggregation function to those columns such as MAX(surname).
The syntax for CREATE VIEW is here.
The syntax for SELECT is here.
I'm looking through our data and there's a handful of tables in our oracle database that show up with two one to many relationships: http://i.stack.imgur.com/icGcV.png
I'm not sure why this would be happening, and is it something I should look into getting changed or fixed?
(I did not create this database, I am only trying to understand it!)
Too long for a comment, let's see a very simple example:
CREATE TABLE persons
(
id NUMBER PRIMARY KEY,
name VARCHAR2(10)
)
/
CREATE TABLE marriages
(
wife NUMBER REFERENCES persons(id),
husband NUMBER REFERENCES persons(id)
)
/
CREATE TABLE dogs
(
id NUMBER PRIMARY KEY,
name VARCHAR2(10),
owner NUMBER REFERENCES persons(id)
)
/
Here you have one table with two different FKs to the same table. At the same time you have another table with a single FK to the same table.
So, it's not a problem to fix, but a part of DB design to understand;
your DB can be well or bad designed, but the existence of such situations does not say anything about that.
I have three tables, Question, SubjectType and CoreValue.
Question table has many to many association to SubjectType table and CoreValue table.
I want to make 2 join tables: one between Question and SubjectType and one between Question and CoreValue.
How do I make sure that the Association tables with CoreValue FK and Question FK gets filled without inserting any values in Corevalue? CoreValue Table already have the values that is needed. I just need to be able to have FK on Question and Corevalue in same association table without inserting any data same goes to Question and SubjectType.
Thanks for advice!
Best Regards!
Just create the tables as pure join tables in the database.
EF will generate a model with navigation properties (Question - SubjectTypes, and so on). You probably want to remove the associations SubjectType.Questions and CoreValue.Questions.
See also this tutorial (the Class-Student part).
In a database I have the tables User, Group and UsersInGroup and generate the entities from the database using the Symfony 2 console. The table UsersInGroup is not generated as an entity but is partly generated into User and Group. This would be perfect if UsersInGroup hat only two columns userId and groupId (together primarykey), in this case the table UsersInGroup also contains a third column Role. This field is can never be filled using the generated entities
How should I fill the Role column in the tabel UsersInGroup?
You have to declare your relation table manually and to map the relations.
You'll find an interesting discussion here:
Doctrine2: Best way to handle many-to-many with extra columns in reference table