Laravel relationship for user with multiple scenarios - laravel

So, i need to make relationship between users table, roles and sections table, the table structure as follows :
users
id - integer
name - string
role_id - foreignId
section_id - foreignId
sections
id - integer
section - string
department - string
roles
id - integer
title - string
User model :
<?php
namespace App\Models;
class User extends Authenticatable
{
use HasFactory, Notifiable;
public function section()
{
return $this->belongsTo(Section::class);
}
public function role()
{
return $this->belongsTo(Role::class);
}
}
The model above is OK if the user only belongsTo one section.
The problem is user can belongs to more than one section depending on their role.
The role itself determine this matrix below
How can i achive this ?

Related

Best Practice to Get Roles of Active Users = Auth::user()

I have 3 table
users
roles
role_users
this table created by Orchid Platform, i want to get role name of Auth::user() in Dashboard Laravel 8 (/home) from role_users table, i have tried to add role_id to users table, thats realy work.
But i want use role_users, because Orchid Platform use role_users.
Please help me, thank you.
I don't know about "Orchid Platform" but that User<->Role (ManyToMany) relationship is a pretty common database structure.
users
id - integer
name - string
roles
id - integer
name - string
role_user
user_id - integer
role_id - integer
User Model:
class User extends Model
{
/**
* The roles that belong to the user.
*/
public function roles()
{
return $this->belongsToMany(Role::class);
}
}
Role Model:
class Role extends Model
{
/**
* The users that belong to the role.
*/
public function users()
{
return $this->belongsToMany(User::class);
}
}
To get roles of the authenticated User
$user = User::find(1);
foreach ($user->roles as $role) {
//
}
If your users will only have 1 role, you can just use a method in your User model to get first role of the User.
public function getRole(){
return $this->roles()->first();
}
Now, you can used this way:
Auth::user()->getRole()->name

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

Polymorphic Relations in Laravel naming convenstion

I am going to create polymorphic relations in Laravel but my tables are so old and it's naming conventions are not according to laravel. Can I do this and how ?
Of course you can set your table name and FK column names directly.
Look over Realtion docs and if necessary in Laravel API or source code
If you have
posts
id - integer
title - string
body - text
comments
id - integer
post_id - integer
body - text
likes
id - integer
likeable_id - integer
likeable_type - string
Then your code will be
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Like extends Model
{
/**
* Get all of the owning likeable models.
*/
public function likeable()
{
return $this->morphTo('likeable', 'likeable_type', 'likeable_id');
}
}
class Post extends Model
{
/**
* Get all of the post's likes.
*/
public function likes()
{
return $this->morphMany('App\Like', 'likeable', 'likeable_type', 'likeable_id');
}
}
class Comment extends Model
{
/**
* Get all of the comment's likes.
*/
public function likes()
{
return $this->morphMany('App\Like', 'likeable', 'likeable_type', 'likeable_id');
}
}

How to create relate one table with others using Eloquent ORM

User TABLE:
id -> int
email -> varchar
Tag TABLE:
id -> int
name -> varchar
tags_user TABLE
tab_id ->foreign('tag_id')->references('id')->on('tag');
user_id ->foreign('user_id')->references('id')->on('user');
how to get all tags from specific user?
thanks
You need many to many relationship:
In your user model:
class User extends Eloquent {
public function tags()
{
return $this->belongsToMany('Tag');
}
}
In your Tag model:
class Tag extends Eloquent {
public function users()
{
return $this->belongsToMany('User');
}
}
Then from your controller:
Utilize the many to many relationship.
$tags = User::find(1)->tags;
Important:
Three database tables are needed for this relationship: users, tags, and tag_user. The tag_user table is derived from the alphabetical order of the related model names, and should have user_id and tag_id columns.
Reference:
http://laravel.com/docs/eloquent#many-to-many

How to perform a has many through statement using laravel eloquent?

Say i have three tables users,users_groups and groups.How can i get the user's groups through the users_groups table?
class User extends Eloquent {
protected $table = 'users';
}
class Groups extends Eloquent {
protected $table = 'groups';
}
class Usergroups extends Eloquent {
protected $table = 'users_groups';
}
As per your request, I've put together a simple example for your specific use case:
The Tables:
Eloquent suggests creating 3 database tables in such a scenario: users, groups and group_user.
The Laravel documentation states that:
The group_user table's name is derived from the aplhabetical order of the related model names.
The group_user table should contain the columns user_id and group_id which will contain the primary keys of group and user entries. The group_user table is what's known as a pivot table.
This conforms to 3NF (if you're interested).
The Models:
The User model should contain the following code:
<?php
class User extends Eloquent {
//...Other code…
public function groups()
{
return $this->belongsToMany('Group');
}
}
The Group model should contain the following code:
<?php
class Group extends Eloquent {
//...Other code…
public function users()
{
return $this->belongsToMany('User');
}
//...Other code…
}
You do not need a UserGroup model as found in your question.
Usage:
In order to retrieve all groups to which a particular user belongs you would do the following:
$user = User::find($user_id);
$user_groups = $user->groups();
Similarly in order to retrieve all users belonging to a particular group you would do the following:
$group = Group::find($group_id);
$group_users = $group->users();
In order to add/remove a user to a group would would do the following:
$user = User::find($user_id);
//Add a user
$user->groups()->attach($group_id);
//Detach a user
$user->groups()->detach($group_id);
Other:
You should read more in the Laravel docs about pivot tables here and about defining foreign keys (for your pivot table) in your migrations here.
I hope that helps!

Resources