Laravel: Eloquent Relationship - laravel

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');
}

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);
}
}

Check in relationship if a column is true, Laravel

I have 2 tables: roles & users.
In users I have role_id, and I want to check if that role has a column "access_admin_area" on true. If true, I am using a middleware.
Gate::define('admin', function ($user) {
return !empty($user->roles()->where('access_admin_area', true)->first());
});
From User model:
public function roles()
{
return $this->hasOne(Role::class);
}
SQLSTATE[42703]: Undefined column: 7 ERROR: column roles.user_id does not exist↵LINE 1: select * from "roles" where "roles"."user_id" = $1 and "role..
The error describes the issue pretty nicely here - the hasOne relationship method inside your User model expects the Role table row to have a user_id column that specifies a foreign key referencing the user table id column.
If I was you, I'd rather use hasMany relationhip between your User and Role model in this use case, since I expect your users and roles should have a many-to-many relationship
check out the many-to-many relationship eloquent and database structure in the laravel documentation https://laravel.com/docs/7.x/eloquent-relationships#many-to-many
Did you checked like this way:
public function roles()
{
return $this->hasOne('App\Role', 'id' , 'role_id');
}
You should change hasOne to belongsTo
then you can a simpler way to save yourselve from many where clauses in your controlleer is by creating another relationship with eg name as rolewithadminaccess
public function roles()
{
return $this->belongsTo(Role::class);
}
public function rolewithadminaccess()
{
return $this->roles()->where('access_admin_area', true)->limit(1);
}
then you can do this in your controller
return $user->rolewithadminaccess;

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 BelongsToMany table relation

I have an issue i can't resolve myself with the documentation.
I want to create a very simple conversation system between two users.
I have 3 models and tables:
User
id , name
Conversation
id
Message
id , user_id , conversation_id , content
So a message belongsTo an user and a conversation but then i want the conversation to belongstomany users.
And i dont know how to make this with the tables. If i create a user_id fields in the conversation table i can't have multiple users...
This is definitely in the documentation
Anyways. You're looking for a many to many relationship. For that you need a pivot table (or junction table) that contains the id of a user and the id of a conversation.
Following Laravel convention this table would be called conversation_user and would have the columns id (primary key), conversation_id and user_id
If you have that you can define the relations like this:
User
public function conversations(){
return $this->belongsToMany('Conversation');
}
Conversation
public function users(){
return $this->belongsToMany('User');
}
Querying the relation
$user = User::find(1);
$conversations = $user->conversations;
As documented here for inserting models into a many to many relation you should use attach()
$conversation = new Conversation;
$conversation->users()->attach($idUser1);
$conversation->users()->attach($idUser2);
// or just pass an array of ids
$conversation->users()->attach(array($idUser1, $idUser2));
I would first of all have the three folowig tables:
User
id , name
Conversation_user
user_id , message_id
Message
id , content
Than let's take care of the relationships.
In your User model:
public function conversations()
{
return $this->belongsToMany(Message::class,'conversation_user','user_id','message_id');
}
In your Message model:
public function users()
{
return $this->belongsToMany(User::class,'conversation_user','message_id','user_id');
}
Now you will be able to call the relationship like so:
$user = Auth::user();
$user->conversations();

Laravel Eloquent - Pivot for three tables

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?

Resources