Laravel Eloquent - Pivot for three tables - laravel

I have the following three tables: Permissions, Users, Clients (the admin).
Users to Clients is Many-to-Many. Permissions are created by my administration, not the Client, but the client can then assign what permission each of its user has.
Now, I want to create a pivot table for this. This client_permission_user would take all three id from the tables. Correct? How do I implement this in Eloquent?
In essence, I want to know how I can create a 3-table pivot table and use Eloquent to get my result. For instance, if this was as 2-table pivot (for instance between Client and User), then the pivot table would have been:
client_user
id
client_id
user_id
And my models would have been
Class Client extends Eloquent {
public function users()
{
return $this->belongsToMany('User');
}
}
Class User extends Eloquent {
public function clients()
{
return $this->belongsToMany('Client');
}
}
So what happens when I have three tables?

Related

Relationship between user and store

I need to create a relationship between user and many stores. I have created a three models
Store Model
id name email phone info
1 xyz xyz#gmail.com 9329292922 Small Store
2 abc abc#gmail.com 9494949449 Some Store
User Model
id name email
1 ewd ewd#gmail.com
2 xcv xcv#gmail.com
User_Store
user_id store_id
1 1
1 2
What does the user_store model contain relations whether it is belongstoMany or hasmany?
You can use belongsToMany relationship
In your Store model define a method
public function users() {
return $this->belongsToMany(Users::class, 'user_store', 'user_id', 'store_id');
}
In your Users model define a method
public function stores() {
return $this->belongsToMany(Stores::class, 'user_store', 'user_id', 'store_id');
}
Looks to me like you want a simple many-to-many relationship.
For this you only need two models User and Store, you only need StoreUser if you want to do something special with the pivot table otherwise it is unnecessary.
The following would be the Laravel way:
Table structure
stores
id
name
email
phone
info
users
id
name
email
store_user
user_id
store_id
Laravel excepts the pivot table to be called store_user, you can read more about it here:
To define this relationship, three database tables are needed: users,
roles, and role_user. The role_user table is derived from the
alphabetical order of the related model names, and contains the
user_id and role_id columns.
Model structure
class User extends Model
{
public function stores()
{
return $this->belongsToMany(Store::class);
}
}
class Store extends Model
{
public function users()
{
return $this->belongsToMany(User::class);
}
}

How to create relationship in Laravel 5 which will collect records of own model mentioned in pivot table?

I have 3 tables users, companies and pivot table with user_id, company_id.
I can't get users, which belongs to my company inside User model.
Tried like
belongsToMany('App\User','companies_users','company_id','user_id' );
but I get relation with wrong users.
Since you are having a belongsToMany relationship between the User and Company, the User belongs to more than one Company. To get users of the companies of a particular User will not be straight forward. If you are sure that is exactly what you want, then do this:
//inside the User model
public function companies()
{
return $this->belongsToMany('Company');
}
//inside the User model
public function companiesusers()
{
$users= new Illuminate\Database\Eloquent\Collection;
foreach($this->companies as $company)
{
$users = $users->merge($company->users->get());
}
return $users->unique();
}
//inside the Company model
public function users()
{
return $this->belongsToMany('User');
}
Then you can get a user's companiesusers like so:
User::first()->companiesusers();

Laravel:Many to Many Relationship

Hello Am trying to create many to many relationship but I failed. I have two table requests_table and Users_table and the relationship is many to many I introduce associative table called request_user with attribute of user_id, request_id and user_reqId(primary key). what I want is to submit data(user_id and request_id) to the associative entity when user request for anything?. help please....
Many-to-many relations are slightly more complicated than hasOne and hasMany relationships. An example of such a relationship is a user with many roles, where the roles are also shared by other users. For example, many users may have the role of "Admin". To define this relationship, three database tables are needed: users, roles, and role_user. The role_user table is derived from the alphabetical order of the related model names, and contains the user_id and role_id columns.
Many-to-many relationships are defined by writing a method that returns the result of the belongsToMany method.
Happy coding:)-
You should try this:
request_tbl.php
public function users() {
return $this->belongsToMany(User::class, 'request_user');
}
user.php
public function requests()
{
return $this->belongsToMany(RequestTbl::class, 'request_user');
}
Controller
$request_tbl = new requests_table();
$users = $request->users;
$request_tbl->users()->sync($users);

Laravel relationship between multiple models

I have the folowing models User, Test, Attempts.
User has access to tests (many-to-many)
public function tests(){
return $this->belongsToMany('App\Test');
}
I have table attempts with folowing columns:
- user_id
- test_id
I need to obtain all user tests with related attempts.
How can I acquire that by using eager loading?
Thanks.
Ensure firstly that the reverse side of the relationship is setup so your Tests Model will require a belongsToMany relationship with Users like so:
public function Users(){
return $this->belongsToMany('App\User');
}
You can specify the table name for the pivot table by passing an additonal argument to the belongsToMany like so :
public function Users(){
return $this->belongsToMany('App\User', 'attempts');
}
To eager load columns from a pivot you can make use of the withPivot method

Laravel: Eloquent Relationship

I have three tables "users", "roles" and "role_users". Apparently, user table does not have any role_id field. Therefore mapping of user with role is maintained in "role_users" table by having role_id and user_id fields.
What association i will need to write in both models User and Role to fetch a list of all users of a particular role e.g.'teacher'?
Here is what i am currently doing in User model:
public function teachers() {
return $this->has_many('Role')->where('name','teacher');
}
This is not working, but i could not understand that how would i make the relationship in this model.
Laravel docs - many to many relation
public function teachers() {
return $this->belongsToMany('Role')->where('name','teacher');
}

Resources