I have a pivot table employee_project. In it, I have id, employee_id and project_id.
Each project has an Account Manager and a Project Manager. So in my form I have name (for the project) which should add the id to project_id and I also have am_id and pm_id, which refer to employee_id. How do I get these IDs into the pivot table since they are named differently than employee_id?
This does not work:
$p = Project::create($request->all());
$p->employees()->sync(['am_id', 'pm_id']);
What should I do differently?
Table Structures
--projects
id
name
stage
status
timestamps
--employees
id
name
department
--employee_project
id
employee_id
project_id
Naming doesnot matter.The values inside array are employee_id anyway. $p->employees()->sync([1,2]); should work.
You are passing am_id and pm_id as string?
Try with:
$p->employees()->sync([$request->am_id, $request->pm_id]);
Related
I have 2 tables in database user_info and product_info.I save user_id,name,email,etc in user_info and product_id,product_name,product_description in product info.Now I have to look for a particular product so how do I get which product was uploaded by whom.How to link user_info with product_info in Android
For this, you have two approaches
you need to create a pivot table having three fields as table user_product_info with fields: id , user_id and product_id. You can set user_id and product_id which is related. here id is a primary key which you can use for further things.
or you can create a field user_id in product_info table and can use that.
Hope it helps you.
I have 2 tables one of them stores students and the other one managers
students have name surname,
eventually managers have name and surname too.
I also store added_by in students table to check which manager added the student.
With the query I join tables
$students = \App\Student::with('payment')->with('discontinuities')
->leftJoin('managers','students.added_by','=','managers.id')
->get();
lets say student is
Name Surname Added_by
Jon Doe 1
and Manager is
id name surname
1 jane doe
So when I join those 2 tables with the query I give above. My student becomes
Jane doe because of name conflicts in those 2 tables.
Simply manager's name overwrites to student's name since both columns named same.
I could actually join only some columns in managers table however I need this name column to print out added_by column with the managers name
and of course I can change columns name to something else in a table however this would be too much work because I have to refactor each query in my application one by one and also test everything from scratch
so my question is how can I join those 2 tables and prevent conflictions on columns with same name?
Try using this:
$students = \App\Student::with('payment')->with('discontinuities')
->leftJoin('managers','students.added_by','=','managers.id')
->select('students.*','managers.name as m_name','managers.surname as m_s_name','managers.other_column_if_needed','managers.another_column_if_needed')
->get();
This way your managers table's name column will be treated as m_name and surname column will be treated as m_s_name
I ran into a similar issue earlier today, the 'get' method takes in an array of the column names which you want in the result as parameter. Therefore to prevent the conflicts you have to give those columns an alias name like so :
$students = \App\Student::with('payment')->with('discontinuities')
->Join('managers','students.added_by','=','managers.id')
->get(['students.*', 'managers.name as managers_name', 'managers.surname as managers_surname', 'managers.other_column_if_needed']);
My guess would be that you should specify column names and tables in the get() part of your query.
example:
$students = \App\Student::with('payment')->with('discontinuities')
->leftJoin('managers','students.added_by','=','managers.id')
->get(['managers.Name', 'managers.Surname', 'students.name', 'students.surname']);
I´m creating a Mysql database where the user table is implemented for login/access purposes and i need to create an employee table with current and past employees. The current employees should have a (1 to 1)corresponding user, past employees shouldn´t. What would be the best way of doing this? Is the design correct?
Thanks
My approach would be to have an employee table with an employee_id Primary Key. This contains current and past employees. I would then have a user table with an employee_id field which has a foreign key relationship with employee.employee_id.
When employees leave the record from user can be deleted to remove their access, while keeping their record in the employee table.
You can make an attribute 'status' in employee table and assign 0 for past employees and 1 for present employees.
Take the employee_id as foreign key in Users table.
Make a join and check status at the login if the status is 1 then proceed to login.
I need a help with a PHP/MySQL issue. I have a table named users and other named relationships.
users
--------------
id (PK)
name
email
etc
relashionships
--------------
id (PK)
id_user (FK to users.id)
id_friend (FK to users.id)
rating
I'm trying to INSERT multiple relationships but I don't want duplicated entries. I want to ignore the current row if the row is duplicated. I can't use the IGNORE statement because the id_user and the id_friend columns aren't unique. A user/friend may have multiple relationship rows.
Any tip?
You can create a unique key on the id_user/id_friend tuple. Neither of them are unique, but their combination is.
See multiple column indexes on the documentation.
Thanks amenadiel, I found that solution here and worked for me!
CREATE UNIQUE INDEX relation ON relationship (id_user, id_friend)
I made three tables.
Table1=users.And the column names are userid (auto_increment) and username.
Table2=hobbies. column names are hobbyid (auto_increment) and hobbyname.
Table3=users_hobbies. column names are FK_userid and FK_hobbyid.
Now whenever I register new user and his/her hobbies from a html form, I select the
corresponding userid and hoobyid that is generated from table 1 and table 2
and insert them to table 3 using query
So what is the use of relationship, if I create it between table 1 and 3 and table 2 and 3?
Will the corresponding userid and hobbyid automatically go to table 3 without using query?
No, the userid and hobbyid won't go automatically anywhere.
The major point of relationships or rather constraints is to enforce data integrity. That means you shouldn't be able to add an entry containing id 2, 2 into the users_hobbies table without a user with id 2 and a hobby with id 2.
In order to keep this integrity you can also specify cascadings. (Depending on the Database system, I hardly work with mysql, so I am not sure about that).
That means, you can specify that all users_hobbies for user with id 1 are deleted if the user himself is deleted.