I expanded Sentry users table with few columns, but in this case I need help for city_id column. I did it in Laravel migrations. Now I need to make relationship between Sentry users and cities table.
I try to expand Sentry user Model and add belongsTo, but I cannot use it from Sentry::findUserById() like Sentry::findUserById()->with('city')
How can I expand Sentry user model with relationship to another table/model?
class User extends Eloquent {
public function city() {
return $this->hasOne('City', 'id', 'city_id');
}
}
Then you should be able to do something like this in your controller
$city = User::find($id)->city->name;
Or in your view you can do
{{ $user->city->name }}
Related
I have 2 tables: roles & users.
In users I have role_id, and I want to check if that role has a column "access_admin_area" on true. If true, I am using a middleware.
Gate::define('admin', function ($user) {
return !empty($user->roles()->where('access_admin_area', true)->first());
});
From User model:
public function roles()
{
return $this->hasOne(Role::class);
}
SQLSTATE[42703]: Undefined column: 7 ERROR: column roles.user_id does not exist↵LINE 1: select * from "roles" where "roles"."user_id" = $1 and "role..
The error describes the issue pretty nicely here - the hasOne relationship method inside your User model expects the Role table row to have a user_id column that specifies a foreign key referencing the user table id column.
If I was you, I'd rather use hasMany relationhip between your User and Role model in this use case, since I expect your users and roles should have a many-to-many relationship
check out the many-to-many relationship eloquent and database structure in the laravel documentation https://laravel.com/docs/7.x/eloquent-relationships#many-to-many
Did you checked like this way:
public function roles()
{
return $this->hasOne('App\Role', 'id' , 'role_id');
}
You should change hasOne to belongsTo
then you can a simpler way to save yourselve from many where clauses in your controlleer is by creating another relationship with eg name as rolewithadminaccess
public function roles()
{
return $this->belongsTo(Role::class);
}
public function rolewithadminaccess()
{
return $this->roles()->where('access_admin_area', true)->limit(1);
}
then you can do this in your controller
return $user->rolewithadminaccess;
The idea is that I have relation between two table's section and user
the section_id exist in users table it relation with id section
SO for ex : section_id = 2 > want to bring all users belongs to id section
My code here use id from url and I don't want to do this I want without id bring all users :
public function getAllUsers($id)
{
$data = User::where('section_id',$id)->get();
}
If I'm understanding you correctly, you want to get all the users for a particular Section that have the relationship to that section.
Assuming you have the relationship set up correctly on the Section model:
public function users() {
return $this->hasMany('App\User');
}
I suggest you go about this 'backward' and just pull the users from the Section model itself (eager loading allows one query):
$section = Section::with('users')->first();
$usersForThisSection = $section->users;
If you setup a One to Many relationship you can say things like: $user->section to return the section a user belongs to...and $section->users to get a collection of all the users in a section -- the getAllUsers() is redundant in this case.
Make sure your User and Section models contain these methods. Sounds like your migration was setup properly if you have a section_id column on your users table.
More info on one-to-many here: https://laravel.com/docs/5.8/eloquent-relationships#one-to-many
// app/User.php
class User extends Model
{
public function section() {
return $this->belongsTo('App\Section');
}
}
// app/Section.php
class Section extends Model
{
public function users() {
return $this->hasMany('App\User');
}
}
Test out your relationships in php artisan tinker like so...
$user = User::find(1);
$user->section
$section = Section::find(1);
$section->users
you need a unique identifier to receive section_id's users. receive it from url(get) or ajax(post) . if you worry about users access then use "auth"
I have two models User and Role
User:
public function roles() {
$this->belongsToMany(Role::class)->withPivot('note');
}
Role:
public function users() {
$this->belongsToMany(User::class);
}
When I add another role to the user:
$role = $user->roles()->save($some_role, ['note' => 'my note']);
I would like to be able to access the pivot id on the role that I just saved like this:
$role->pivot; //null
What am I doing wrong?
You can access elements of a pivot table via the related model.
in your example it would be
echo $user->roles()->find($some_role->id)->pivot('note');
//-> my note
not knowing how your tables are structured I can't help further than inviting you to use php artisan tinker
The pivot is not available on $role because it is not a role object. It should be available on $some_role. Try this:
$some_role->pivot;
I have 3 tables users, companies and pivot table with user_id, company_id.
I can't get users, which belongs to my company inside User model.
Tried like
belongsToMany('App\User','companies_users','company_id','user_id' );
but I get relation with wrong users.
Since you are having a belongsToMany relationship between the User and Company, the User belongs to more than one Company. To get users of the companies of a particular User will not be straight forward. If you are sure that is exactly what you want, then do this:
//inside the User model
public function companies()
{
return $this->belongsToMany('Company');
}
//inside the User model
public function companiesusers()
{
$users= new Illuminate\Database\Eloquent\Collection;
foreach($this->companies as $company)
{
$users = $users->merge($company->users->get());
}
return $users->unique();
}
//inside the Company model
public function users()
{
return $this->belongsToMany('User');
}
Then you can get a user's companiesusers like so:
User::first()->companiesusers();
I have two types of users: Personal and Business. Personal user can belong to Business user. Thats what i created pivot table personal_users_business_user that have user_id,business_user_id.
I have User,PersonalUser and BusinessUser model.
In User model i have this relationship:
public function company()
{
return $this->belongsToMany('App\Models\PersonalUser','personal_users_business_users', 'user_id', 'business_user_id');
}
Im using attach() method :
$user->company()->attach($business_user_id);
It store data in pivot table. Now what i want in view is something like this:
<input class="typeahead form-control" type="text" name="user_company" #if($user->company->approved == 0) ? disabled : '' #endif value="{{$user->company->company_name}}">
But i dont how how to access to those fields from user in pivot table. Any suggestion?
If you need that Business user have multiple personal users, you should use hasMany, belongsTo relationship. In your BusinessUser model you should create method:
public function personalUsers() {
return $this->hasMany(PersonalUser::class);
}
And in your PersonalUser model you should define this method:
public function businessUser() {
return $this->belongsTo(BusinessUser::class);
}
In your migration, for personal users table add following field:
$table->integer('business_user_id')->unsigned()->nullable();
That will make your code right. You don't need pivot table for that relationship.
Set relationship like this
public function company()
{
return $this->belongsToMany('App\Models\PersonalUser','personal_users_business_users', 'user_id', 'business_user_id')
->withPivot(['your_pivot_column']);
}
And now when you code
$user = User::with(['company'])->get();
$user will have the company relationship with the intended pivot column.