want to view logged user company name, not id - php

I have two related Tables $table->foreignId('company_id')->references('id')->on('companies');
users Table
companies Table
id
id
company_id
name
Second
in view I want to show logged user company name
{{Auth::user()->company_id->name}}
pls help : (

Assuming that many users can belong to a company, you'll want to define a One to Many relationship between your User and Company models.
On your User model, add the following method.
public function company()
{
return $this->belongsTo(Company::class);
}
Retrieving the name of the company for the currently logged-in user is done like so.
$name = Auth::user()->company->name;

You can use this code
{{Company::find(Auth::user()->company_id)->name}}
where Company is your company's model or you can define a one-to-one relationship in your User model like so
/**
* Get the company associated with the user.
*/
public function company()
{
return $this->belongsTo(Company::class);
}
And use it like this
{{Auth::user()->company->name}}

Related

Laravel adding a role to a newly created user

I am creating a new user from my already made CRUD, however, I don't know how I can also assign a role to the user we are creating. I am working with Laravel and 3 tables. I have 1 table that is my users table, 1 table that is my roles table, and 1 table that is my bridge table between roles and users. I have this made because my users can have more than 1 role assigned to themselves.
My question now is how do I give the user a role and how do I save the user to all the tables I mentioned before. Please remember I am working from a crud, so I have a create view.
Greetings and thanks for all the help!
Assuming you have these tables:
users
roles
role_user
Where role_user has a role_id and a user_id column. Then your models would look something like:
class User extends Model
{
/**
* Roles relationship.
*
* #return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function roles(): BelongsToMany
{
return $this->belongsToMany(Role::class);
}
}
class Role extends Model
{
/**
* Users relationship.
*
* #return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class);
}
}
And because it's a BelongsToMany relation, you would use the sync or syncWithoutDetaching methods to add or synchronise user roles. Or attach() for adding a single role to a user. For example:
// The $role variable can be a model instance, or an ID of the role.
$role = Role::all()->random();
$user = User::all()->random();
$user->roles()->syncWithoutDetaching($role);
// or
$user->roles()->attach($role);
I would recommend looking at the documentation for managing the relations (link). Because sync for example removes relations that aren't provided in the given array of models or ID's to synchronise - which would result in an unexpected behaviour if you aren't aware of this behaviour.
I fixed it with the solution written above.

Laravel Eloquent accessing a 3rd relationship

I have 4 models, User, Profile, Audit and AuditApplications.
My table structure looks something like:
User
- id
- ... other user-related fields
Profile
- id
- user_id
- ... various profile fields
Audit
- id
- user_id
AuditApplications
- id
- audit_id
- user_id
My User model looks like this:
public function profile()
{
return $this->hasOne('App\Profile');
}
public function auditApplications()
{
return $this->hasMany('App\AuditApplications');
}
So I can get the user and their related profile easily and also a list of audits a user has applied for.
Next, I have AuditApplications table which holds a list of user IDs and audit ids of users that have applied to an audit. My AuditApplications table has the following relationship.
public function user()
{
return $this->belongsTo('App\User');
}
So I can return a list of users that have applied to an audit like so:
$applicants = AuditApplications::where('audit_id', $audit->id)->with('user')->get();
All this works well as expected but now I want to access the user's profile when I get a list of applications. The relationship is a distance one so I want to get a list of users and their profiles in the AuditApplications model.
Is there a way of doing this in Eloquent without creating a loop in my controller to then loop over each of the users getting their profiles individually?
you just need to do ->with('user.profile')

Laravel relations relation

I have an understanding issue with Laravel Eloquent Models and relations.
I have a user table user. These users can add car license plates, which I save in user_plates. I have a database which contains car models and types. The table name is cars. I have the models App\User, App\UserPlates and App\Car.
The user_plates table has the fields id,plate,user_id,car_id.
I save the plate, the associated user (in user_id) and the selected car (car_id (id from table cars))
I added plates() with belongTo function to my User Model which already successfully returns all plates associated with that user. But now I want to get the associated car (car_id inside user_plates). How do I achieve this using Eloquent? The car table does not have any connection to the user table, only user_plates has a car_id and a user_id.
I need to achieve this:
User -> Plates (can be multiple) -> Plate -> Car. I know how to achieve this using simple MySQL Joins but I want to do it right with Eloquent. Thanks for any help!
Laravel: 6.4.0
So, if your database is set up as...
users user_plates cars
----- ----------- ----
id id id
etc. plate etc.
user_id
car_id
Your models are set up as...
// in model: User
public function user_plates()
return $this->hasMany('UserPlate'); // fill out fully qualified name as appropriateā€¦
// in model: UserPlate
public function user()
return $this->belongsTo('User');
public function car()
return $this->belongsTo('Car');
// in model: Car
public function user_plates()
return $this->hasMany('UserPlateā€™);
To return a collection of cars belonging to user $id you should be able to run:
$cars = User::findOrFail($id)-> user_plates->pluck('car');

How do I access one model from another which don't have direct relationship?

I have an User model. Each user has certain grades and each grade has certain students.
User and Grade models have one-to-many relationship. Grade and Student models have one-to-many relationship too. But, User and Student models don't have any relationship.
Once User is logged in, he should be able to search all students that belongs to the user.
Do I have to create relationship between these two models to?
If I understand the question correctly, a user has multiple students, so what I'd do is create a one-to-many relationship between them like this:
//User.php
public function students()
{
return $this->hasMany(Student::class);
}
//Student.php
public function user()
{
return $this->belongsToOne(User::class);
}
This would allow you to pick up a users students by doing: $students = $user->students Note: I didn't use () here. If you were to execute $students = $user->students() you'd end up with a query builder object.

Eloquent relationship between user roles

I am creating an app where users have different roles. Some of the roles include:
Administrator
Supervisor
Contractor
I have the following tables setup:
user
role
role_user
The role_user is a pivot table used to store the user_id and role_id.
Now, when a Contractor is created, I trigger an event which assigns the user to the Contractor role in the database:
$user->roles()->save($role, ['cust_id' => $event->user->cust_id]);
The above line basically just creates a new row in the role_user pivot table and points to the role_id of a Contractor. This results in the user having Contractor privileges.
Also, within the event I need to assign the user to a Supervisor. However, there is no Supervisor model. Supervisors are also stored in the user table and have a role of Supervisor assigned to them.
How can I create a relationship between Contractors and Supervisors?
Ideally, i'd like to use something like this to create the Contractor/Supervisor relationship in the pivot table:
$user->supervisors()->save($supervisors, ['cust_id' => $event->user->cust_id]);
But i'm not sure how to setup my model...?
/**
* The supervisors that belong to the user.
*
* #return Object
*/
public function supervisors()
{
return $this->belongsToMany('SimplyTimesheets\Models\User\User')->withTimestamps();
}
What your asking is possible, however your relationship model would look like this:
public function cands()
{
return $this->belongsToMany('Some\Model')->where('something', $something)->where('something', $something);
}
You would have to use where clauses to hard code a relationship between contractors and supervisors because there is no direct relationship between them both.
/* Edit */
From your comment, you would have to approach that query with eager loading your relationship like so:
User::has('supervisors')->join(...)->where(...)->get();
The query above would return all users which meet the roles criteria. If you was just after one result you could create a hasOne relationship with your where clauses.
public function relationship()
{
return $this->hasOne('Your\User_Role\Model', 'foreign_key', 'other_key')->where('role_id', supervisor)->where('role_id', contractor)
}
It all comes down to your application needs.

Categories