My Application have below setup
Users table:
id
roleid
firstname
email
password
Roles table:
id
userid
rolename
There will be total 4 fixed roles and those will be assigned to users.
Now i want related role name when i do login with user.So in laravel eloquent model how can i define the relationships and how can i fetch and display?
I am new to laravel 5.4
If your relation between user and roles is One To Many
Have a read on below link. You'll get the idea.
https://laravel.com/docs/5.4/eloquent-relationships#one-to-many
Related
Roles tables only two roles Admin and User only. ( Static table)
Admin
User
User tables - whenever user register the page, it will automatically comes (2- User (Roles table)
Ex.
Si.no . Name address email rollid
1 xxx yyyy zzzz 2-(Roles table ) Users only registered so Users no 2 come from Roles table
Doubt?
What is the relationship between them
ManyTomany or Onetoone
. Many users have many roles. But my case one user object have one role only.. Which one is correct? Then how can insert value 2 to user table whenever user register the page. Please advice
Two tables is enough no need for third table
I have Model name Project and User by using laravel relationship.
- project and user using many to many relationship.
What i want is using laravel authentication giving roles to the user base on project.
Which mean :
user "Ali" have role "admin" in project "project a" ,
but in project "project b" the user "Ali" role is "member".
How can i implement it?
Problem fixed :
My solution is add additional data inside the project_users pivot table store as string role.
When create new project and add with additional data using :
$user->projects()->save($project, ['role' => 'Manager']);
When need to call it :
$user_role = $user->projects()->where('user_id', $user['id'])->first()->pivot->role;
It will return a string 'Manager'
and use it to assign the role.
$user->syncRoles($user_role);
Look at the following links which will help you to achieve it.
https://github.com/spatie/laravel-permission
https://www.itsolutionstuff.com/post/laravel-56-user-roles-and-permissions-acl-using-spatie-tutorialexample.html
You can add the Role data e.g. role_id with a separate Role table or just role in Project To User Pivot Table project_users and retrieve the role info from this relationship.
Check out this Saving Additional Data On A Pivot Table section at Update Many To Many Relationship Laravel Documentation.
I have USER & ROLE Table
I want to show the role of that particular user on his datatable which is in Vue.
All the related codes are given on those links.
Thanks in Advance.
PLEASE MENTION IF YOU KNOW ABOUT ANY VIDEOS/TUTORIALS ON THIS TOPIC
Vue Code link: https://notepad.pw/code/nqxo2q43
Roles Table: https://notepad.pw/code/n3zw1m9jh
Users Table: https://notepad.pw/code/5yrleots
Role Model: https://notepad.pw/code/o853sl6fv
User Model: https://notepad.pw/code/f00vu7m1
Role Controller: https://notepad.pw/code/w773twp2p
User Controller: https://notepad.pw/code/4vzarmyk
In my database, there is two table members and services. in that table, there are 5 column
members
Username, Rank, Name, Password, and more
services
username user_services
I don't want my default login from AUTH controller from users table but I want login from my members table and authorize table from another table services
if user logins authentication from members table and authorize from services table
Tried everywhere but couldn't get any valuable answer.
Thank You.
To change de auth table
Edit app/config/auth.php to change the table.
'table' => 'members',
If you need to change field in app/Http/Controllers/Auth/LoginController.php
public function username()
{
return 'yourField';
}
And you can continuos using AuthController from Laravel
I’d like to add session variable to Auth when the user logs in.
Where should that code be exactly?
Here’s my structure:
Group table (parent table):
Id
Currency
Client_type
User table (child table):
Id
Group_Id (FK)
Name
I’d like for the values of Currency and Client_type to be available globally in Auth.
Thanks, using Laravel 5.2
Auth::user() will have the logged in user object from which you should be able to access all its properties.
edit: if the currency is a property of a related model then you should have your relations set up, maybe something like this in the User model:
public function group()
{
$this->belongsTo(Group::class,'Group_Id');
}
and you access it then like so
Auth::user()->group->Currency;
BTW I'm not sure how the tables are related.