I have on my database these 3 tables:
users (model User)
accounts (model Account)
user_accounts (relationship table many to many) (id, user_id, account_id, role_id)
The problem is in the user_accounts I have an additional column role_id the meaning is to invite many users with different access levels.
maybe using $user->accounts()->sync($account, false);
how to save the data in this table maybe using
From the documentation, you can do it as follows:
$user->accounts()->sync([$accountId => ['role_id' => $roleId]], false);
Related
i have 3 tables and 1 tables pivot with 3 foreign keys , i want to show all artists name with theirs job for one media but i dont have any idea how about joinning those 3 tables
Media table: media_id , media_title
artist table: artist_id, artist_name
job table: job_id, job_name
pivot table: media_id, artist_id, job_id
I have tried to do with belongsToMany between them but no result, only show the artists name but can not get theirs job name for each
Thank you to much!
Can you give the relation between these tables?
I didn't really know what you want, but try this.
\DB::table('media_artist_job')
->join('media','media.media_id','=','media_artist_job.media_id')
->join('job','job.job_id','=','media_artist_job.job_id')
->join('artist','artist.artist_id','=','media_artist_job.artist_id')
->select([
'media.medial_title',
'job.job_name',
'artist.artist_name'
])->where('media.media_id',/*The media id you want to retrieve*/)->get();
Hope it help!
I have some relations on my db and I'm very confused how I can do this with eloquent. I have three tables that I want to connect eachother.
Users table - Contains users and these users have 3 roles (customers, managers, employee etc.) And users with manager role have more than one customers or employes.
Comps table - Contains company infos, (customers company, manager company, employee company etc.) employes can have same company.
Images table- this table contains user profile images, comp logo images etc.
I want to take a user's customers informations with images. I made the following relation between comp table and user table in User model. But I can't take comp image with this relation.
public function customers() {
return $this->belongsToMany('App\Comp', 'user_customers', 'user_id', 'comp_id');
}
user_customers table is a relation table which shows which user is a customer of which user.
user_customers table:
id,
user_id,
customer_user_id,
comp_id,
How can I get comp's logo image?
There are two tables 1. employess (column emp_name, emp_dept, emp_address ) 2. department (dept, role)
I have to fetch the records from employee table
based on department(admin, user, emp) and respective role(1,2,3,4,5) from employee and department table.
If employee is admin then records with role 1 and 3 should be fetched if user then only records with role 5.
Please help me to write the query.
Following is the query which I tried:
select emp_name, emp_dept,
(select role from department d where d.dept= e.emp_dept) role, emp_address
from employee e
where role IN (case emp_dept
when 'admin'
then('1','3')
when 'user'
then ('5')
when 'emp'
then('4')
end
)
Could be good to start with a Users table , where you will store all users independently if they are admin or employee . After that try to write a draft of E/R model , to make us understand what you need .
I'm using Laravel 5.3. I've 4 tables.
Default Users table. Departments, Position, Employees tables.
Users table has ID | Email | Password
Departments table has ID | Department | User_Id - Here User_Id is foreign key comes from Users table's ID
Positions table has ID | Position | Department_Id - Here Department_Id is foreign key comes from Departments table's ID
Employees table has ID | Employee | Position_Id - Here Position_Id is foreign key comes from Positions table's ID
User can have multiple Departments. Departments can have multiple Positions, Positions can have multiple Employees. So, if user is different, how can i retrieve all data from all 4 tables which that user had created?
You can use nested eager loading:
$departments = Department::where('user_id', $id)
->with('positions', 'positions.employees')
->get();
Another way is to build simple queries:
$departments = Department::where('user_id', $id)->get();
$positions = Position::whereIn('department_id', $departments->pluck('id'));
$employees = Employee::whereIn('position_id', $positions->pluck('id'));
I have a Users table and a Votes table and a vote_users table (many to many relationship)
In the vote_user table I have
user_id|vote_id| anonymous.
How can I change the user_id to -1 (only when sending to client) , if anonymous column has value TRUE ?