Get formatted attributes from a related table for eloquent model - laravel

I have 3 tables
1. User table[id, name, email]
2. user_roles[user_id, role_id]
3. roles[ id, permission{post:{edit:true,delete:false}} ]
user hasone user_roles
roles belongs to user_roles
userRole.role is eager loaded in user model
Is it possible to get the permission by calling some custom function in user model.
instead of user->userRole->role everytime.
like call like $user->permissions() will return json from roles tables

You can create a method in your User model like this-
public function permissions()
{
return $this->userRole->role;
}
Laravel allows you to call your relation like that. Here $this represents your User model where you created this method. So calling a relation on $this would work without any problem.
Then you can call this with your user collection like this-
$user->permissions();
If you want to use this with logged in user then you can call it like this-
auth()->user()->permissions();

Related

laravel prevent overload data on Auth::user()

My model User have some relation: profile, activites, etc...
on my controller i have this:
$user = Auth::user();
$user->load(['profile', 'activities']);
excpected result on my blade is that my variable $user is with my relationships and the Auth::user() data remain the users table data only;
i have a layout layout.blade.php and a view index.blade.php that extends my layout;
in my index i want to use the relation data and in my layout i want only the users data; but i always have the relation loaded from controller
If you load any relationship, laravel will add that relationship in your user object automatically.
If you don't want this and want the user only, you can duplicate the object first to another variable before loading any relation.
Second option, you can get the user again
If you want to have the same object but without the relationship you have multiple choices:
$user->unsetRelation('profile');//unset specific relationship on the $user Object
$user->unsetRelations();//unset all relationships on the $user Object
$userWithoutRelationships = $user->withoutRelations();//clone the current Object but without relationships

Laravel Query Builder vs Laravel Eloquent ORM in many to many relation

Hi i want to get same result in join query builder as we get in many to many eloquent orm..
$users = DB::table('users')->join('role_user','users.id','=','role_user.user_id')
->join('roles','roles.id','=','role_user.role_id')
->get();
if I use this then I got separate array for same user with role.. for example XYZ user has 3 role then it give me 3 array of same user.
but I want to get it like this user has 3 roles like eloquent
you can use laravel relationship in view file for that you need to define relationship in user model
public function userRoles(){
$this->hasMany(UserRole::class);
}
in controller you need to use With that eager load userRole
To get user roles in view file
$user->userRole
// this will return array of user role related to that user

How to check and get the result if the table contain user id in laravel

I have create a relationship between user and table column, I want show the table list which is belongs to particular user. For example if user_id 1 is logged in the system, the system will only show the information belong to him which is Table 1.
This is my controller code :
public function show(Request $request){
$user_id=Auth::user()->id;
$table= Roundtable::findOrFail($user_id);
return view('users.tables.show')->withTables($table);
}
I know that $table= Roundtable::findOrFail($user_id); is incorrect but I had no idea how to do because I am new for laravel.
If user has just one table and if Roundtable model has user_id you can use this query:
Roundtable::where('user_id', $id)->first();
It will give you user's table or null if table doesn't exist.
Another way to get table is to use relation:
auth()->user()->roundTable;
Well i found the solution, just need to change the code into
$id=Auth::user()->id;
$table= Roundtable::where('user_id',$id)->get();
return view('users.tables.show')->withTables($table);
then the result will return correctly.

How do I get a assign a record value to a variable in Laravel 5.3?

I need to display all the vehicle records belonging to properties of the currenty logged on user.
Before I pass the data to the view I need to filter out all the properties except those owned by the current user id.
I have a relationship in the Vehicle model that performs the foreign key constraint.
public function propiedad(){
return $this->belongsTo('App\Property', 'propiedad_id','id' );
}
My Controller is using this filter before passing the data to the view. I assume I have to acquire the property ids that belong to the user before I filter the vehicles that belong to those properties. This is where I am having trouble.
$user_properties = array(?????);
$Vehiculo = Vehiculo::where('propiedad_id', $user_properties);
If your properties table has a user_id column then on your user model define a vehicles hasManyThrough relationship like
public function vehicles()
{
return $this->hasManyThrough(Vehicle::class, Property::class);
}
Then in your controller you can do:
$users = User::with('vehicles')->get();
Or for a single user
$vehicles= User::find($id)->vehicles;

User Relationships, Many to Many

I need to create relationships between users. As you might expect, they exist in the users table. My approach is a relationships table linking user_a_id to user_b_id.
Then, in the User model, I added:
public function relationships()
{
return $this->belongsToMany('User', 'relationships', 'user_a_id', 'user_id');
}
Unfortunately, I get an error when trying to find the relationships;
dd($user->relationships);
Output: Whoops, looks like something went wrong.
I would consider something like a "user" model, and a "group" model. The users table can have a many-to-many relationship with the groups table, and this could be defined with a "user_group" table, containing a column "user_id" and another "group_id". Then you can have a function in your User model called groups, which does return $this->belongsToMany('Group');, and a function in your Group model called Users, which goes return $this->belongsToMany('User');
Refer to this section of the Eloquent docs for further direction.

Resources