Laravel Get property with many to many relationship - php

my user model:
public function roles()
{
return $this->belongsToMany('App\Role', 'user_role_pivot', 'user_id', 'role_id');
}
my role model:
public function users()
{
return $this->belongsToMany('App\User', 'user_role_pivot', 'role_id', 'user_id');
}
public function rolepermissions()
{
return $this->belongsToMany('App\User', 'role_permissions_connect', 'role_id', 'role_perm_id');
}
In laravel i want to get request user role permissions, this is my relation in tables. How can i get property of role_permissions?
i tried this one:
$user->roles->rolepermissions->pluck('permission_name')

In Role model your rolepermissions are linked to App\User model:
public function rolepermissions()
{
return $this->belongsToMany('App\User', 'role_permissions_connect', 'role_id', 'role_perm_id');
}
You should change belongsToMany('App\User' ... to belongsToMany('App\RolePermission' probalby.

$user=User::find(1);
foreach($user->roles as $role)
{
foreach($role->rolepermissions as $rolepermission)
{
echo $rolepermission->permission_name;
}
}

Related

Check user if is in chat room Laravel

I have models:
ChatRoomMembers
ChatRoom
so I want to check if auth user is in chat room
my relationships:
ChatRoom:
public function chatRoomMembers()
{
return $this->hasMany(ChatRoomMember::class);
}
ChatRoomMembers:
public function chatRoom()
{
return $this->belongsTo(ChatRoom::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
So I created relationship in User Model:
public function chatRooms(){
return $this->hasManyThrough(
ChatRoom::class,
ChatRoomMember::class,
'user_id',
'id',
'id',
'chat_room_id'
)->orderBy('created_at', 'DESC');
}
and I am getting only this user chat rooms
In user model add below relationship
public function chatRoomMember(){
return $this->hasMany(ChatRoomMembers::class);
}
and in code
auth()->user()->chatRoomMember->count()
or
auth()->user()->chatRoomMember->exists()

One to Many relation in laravel

I have 3 tables users companies company_users when I try to get user company details it return null
Logic
company has many users
user belong to 1 company
user model
public function company()
{
return $this->belongsTo(Company::class, 'company_users', 'user_id', 'company_id');
}
company model
public function user()
{
return $this->belongsToMany(User::class, 'company_users', 'company_id', 'user_id');
}
any idea?
it's not one to many relation it's many to many, with pivot table (company_users )
so, the relation should be like:
public function company()
{
return $this->belongsToMany(Company::class, 'company_users', 'user_id', 'company_id');
}
I recommend rename it to 'companies' cause it plural
Regarding to this comment:
If you really want one to many relation. Try this:
public function company()
{
return $this->hasMany(User::class);
}
public function user()
{
return $this->belongsTo(Company::class);
}

Why when I use belongsToMany relation between two table not get the data

Get data through belongsToMany relation between the tables:
Users Model:
public function favorites()
{
return $this
->belongsToMany('App\job', 'favourites', 'user_id', 'job_id')
->withTimestamps();
}
Job Model:
public function favorites()
{
return $this
->belongsToMany(job::class, 'favourites', 'job_id', 'user_id')
->withTimestamps();
}
Controller:
public function index()
{
$jobs = Auth::user()->favorites;
return view('home',compact('jobs'));
}
Nothing appears any errors and any data.
Check your Job model. Your relationship is pointing to the same (Job) model. Change it to this:
public function favorites()
{
return $this
->belongsToMany(User::class, 'favourites', 'job_id', 'user_id')
// ^^^^^^^^^^^^
->withTimestamps();
}

How to use Multiple relationship in laravel 5.4

In my app i have define relationship (profile, user, level) but when I fetch data it is showing an error (Trying to get property 'email' of non-object) how can i solve this thank in advance.
this is User Model
public function profile()
{
return $this->hasOne(Profile::class, 'user_id');
}
Profile Model
public function user()
{
return $this->belongsTo(User::class, 'id');
}
public function level()
{
return $this->belongsTo(Level::class, 'id');
}
Level Model
public function profile()
{
return $this->hasOne(Profile::class, 'level_id');
}
This is Controller ProfileController
$users = Profile::with(['user', 'level'])->where('is_bd_partner', 'Yes')->get();
foreach ($users as $key => $value)
{
echo $value->first_name.'<br>';
echo $value->last_name.'<br>';
echo $value->user->email.'<br>';
echo $value->level->level.'<br>';
}
Note that belongsTo takes foreign_key as the first parameter.
So you should change the Profile Model as
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
public function level()
{
return $this->belongsTo(Level::class, 'level_id');
}
Read more here

laravel returning relationships with eloquent and laravel

In my database I am an organisations table this table has the following relationships,
Many-to-many with users
Many-to-many with clients
One-to-many with projects
In turn these relationships have other relationships for example projects
One-to-one with client
In my controller I am doing the following,
$organisation = Organisation::all();
$organisation->load('users');
$organisation->load('clients');
$organisation->load('teams');
$organisation->load('projects');
return Response::json($organisation, 200);
So get all the organisations and there relational data.
However what I wanting to do is also get the relationships of relationships, so for example get the client that is related to each project that an organisation has? I thought that doing what I am doing would have worked but obviously not.
Here are my models,
Organisation,
class Organisation extends Eloquent {
//Organsiation __has_many__ users (members)
public function users()
{
return $this->belongsToMany('User')->withPivot('is_admin');
}
//Organisation __has_many__ clients
public function clients()
{
return $this->belongsToMany('Client');
}
//Organisation __has_many__ projects
public function projects()
{
return $this->belongsToMany('Project');
}
}
Projects
class Project extends Eloquent {
protected $fillable = [
'name',
'description',
'total_cost',
'start_date',
'finish_date',
'sales_person',
'project_manager',
'client_id',
'organisation_id',
'user_id'
];
public function organisations()
{
return $this->belongsToMany('Organisation');
}
public function salesperson() {
return $this->belongsTo('User', 'sales_person');
}
public function clients() {
return $this->belongsTo('Client', 'client_id');
}
}
Clients
class Client extends Eloquent {
public function organisations()
{
return $this->belongsToMany('Organisation');
}
public function users()
{
return $this->belongsToMany('User');
}
public function projects()
{
return $this->hasMany('Project');
}
}
Have you tried:
$organisations = Organisation::with('projects', 'projects.clients')->all();
foreach($organisations as $organisation) {
foreach($organisation->projects as $project) {
foreach($project->clients as $client) {
echo $client->name;
}
}
}

Categories