I have 2 tables Users, Organization
Users hasMany Organization
Organization belongsTo User
User.php
public function organization()
{
return $this->hasMany(Organization::class, 'user_id');
}
Organization.php
public function user(){
return $this->belongsTo(Organization::class,'user_id');
}
Now I want to retrieve data of Organization with user
In Controller
dd( Organization::with('user')->get() );
but in relation, user returns null. what should I do now? Please help
In the Organization model the user relationship should be a belongsTo with User not Organization(self).
public function user()
{
return $this->belongsTo(User::class);
}
Then you can load that relationship on Organization:
$org = Organization::with('user')->get();
Related
I have 2 models: one for users and one for clients. A user is a customer
User has a 'codigocli' field and client has a 'codigo' field
The relationships between my models are like this:
//User model
public function cliente()
{
return $this->hasOne(Cliente::class, 'codigo', 'codigocli');
}
//Cliente model
public function user()
{
return $this->belongsTo(User::class, 'codigocli', 'codigo');
}
My database is fine (I think) client has the 'codigo' field and users has the 'codigocli' field. So what am I doing wrong? When I want to query my home.blade.php with dd(auth()->user()-cliente()) I don't get anything, although it shows me the parent object fine.
H
You have an OneToOne relationship here so try this if you don't change id name:
//User model
public function cliente()
{
return $this->hasOne(Cliente::class, 'codigocli');
}
//Cliente model
public function user()
{
return $this->belongsTo(User::class, 'codigocli');
}
if you change id name:
//User model
public function cliente()
{
return $this->hasOne(Cliente::class, 'codigocli','local_id_name' );
}
//Cliente model
public function user()
{
return $this->belongsTo(User::class, 'codigocli', 'local_id_name');
}
I am trying to fetch all data from users and users_profile table with the corresponding id and user_id with using the hasOne relation, But I don't know how to make the condition to fetch data from the two tables with using the corresponding id and user_id
controller
public function index()
{
$profile = Userprofile::with('users')->get();
return view("admin.allusers",compact("profile"));
}
in user model
public function usersprofiles(){
return $this->hasOne(Userprofile::class);
}
in Userprofile model
public function users(){
return $this->belongsTo(User::class);
}
please help me to solve this problem
use the code below:
public function usersprofiles(){
return $this->hasOne(UserProfile::class, 'user_id', 'id');
}
That's assuming you have a field user_id in your users_profiles that correspond to field id in your users table.
But since you are fetching the profiles of users, your index() should be:
$profiles = User::with('usersprofiles')->get();
return view("admin.allusers", compact("profiles"));
And your User Model
public function usersprofiles(){
return $this->hasOne(Userprofile::class, 'id', 'user_id');
}
And your UserProfile Mode
public function users(){
return $this->belongsTo(User::class, 'id');
}
Hope it helps.
I'm implementing a simple user role and permissions model in Laravel, but I'm not sure if the Eloquent relationship I need exists. I want to return a relationship of User's permissions, but that is via the Role model. A user only has one Role via role_id, but a Role has many Permissions.
Users belongsTo => Role hasMany => Permissions
I want an Eloquent relationship method of User->permissions() but hasManyThrough is not the right relationship structure.
Any ideas?
like this.
User
public function permissions()
{
return $this->role->permissions;
}
You can use a HasManyThrough relationship:
class User extends Model
{
public function permissions()
{
return $this->hasManyThrough(
Permission::class, Role::class, 'id', null, 'role_id'
);
}
}
I assume you have model with name User.php Role.php Permission.php
then relationship define as per below
In User.php
public function role()
{
return $this->belongsTo(Role::class, 'role_id');
}
In Role.php
public function permissions()
{
return $this->hasMany(Permission::class, 'role_id');
}
Now use this relationship
public function show($id)
{
$user = User::select('id', 'name', 'email', 'role_id')
->with([
'role.permissions'
])
->find($id);
$permissions = $user->role->permissions;
dd($permissions); //here is your permission list
}
Am still new to laravel
I have the following tables
user
id
first_name
last_name
educations
id,
user_id //references user id
type
Now in my user model i would like to get a specific users educations which can be many
so a user can have many educations but each education belongs to a single user.
So in my user model i have
public function education()
{
return $this->hasMany('App\ApplicantEducation','id','user_id');
}
In my Education model i have
public function user()
{
return $this->belongsTo('App\User','user_id','id');
}
But the above fails, I cannot retrieve user specific educations
Where am i going wrong?
try this:
in User Model:
public function educations()
{
return $this->hasMany('App\ApplicantEducation', 'user_id');
}
in Education Model:
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
Change return $this->hasMany('App\ApplicantEducation','id','user_id');
to return $this->hasMany('App\ApplicantEducation','user_id', 'id'); you also ommit the id and user_id.
As your foreign_key is well formed, you can also rwite this simple code,
class User{
public function education()
{
return $this->hasMany('App\ApplicantEducation');
}
}
Class Educations{
public function user()
{
return $this->belongsTo('App\User');
}
}
Here,
$this->hasMany('App\ApplicantEducation','id','user_id');
In above statement first argument should be Model second should be foreign key and the third one is any other key from Education model.
Here, second and third arguments are not mandatory.
In User Model
class User...{
public function education()
{
return $this->hasMany('App\ApplicantEducation');
}
In Education Model
public function user()
{
return $this->belongsTo('App\User');
}
Here, additional parameters are not mandatory might be your addition parameters creates issue,
and now you can retrieve your user with Education by
$user = User::with('education')->get();
This can retrieve all the users with their education.
I hope it helps,Thank you, Happy coding.
You should try this:
Education Model
public function user()
{
return $this->belongsTo('App\User','user_id);
}
User Model
public function education()
{
return $this->hasMany('App\ApplicantEducation');
}
I have the following model relationships. If a user logs in as an employee, I want them to be able to get a list of employees for a their company and the roles they have been assigned:
class User {
// A user can be of an employee user type
public function employee()
{
return $this->hasOne('App\Employee');
}
//
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
class Employee {
// employee profile belong to a user
public function user()
{
return $this->belongsTo('App\User');
}
// employee belongs to a company
public function company()
{
return $this->belongsTo('App\Company');
}
}
class Company {
public function employees()
{
return $this->hasMany('App\Employee');
}
}
But the following query doesnt work. I get error Column not found: 1054 Unknown column companies.id in WHERE clause:
$employee = Auth::user()->employee;
$companyEmployees = Company::with(['employees.user.roles' => function ($query) use ($employee) {
$query->where('companies.id', '=', $employee->company_id)
->orderBy('users.created_at', 'desc');
}])->get();
The users and the employees table have a one to one relationship.
All employees have a base role type of employee in addition they may also have other roles such as manager, supervisor etc.
How do I write a query that gives me a company with all its employees and their roles?
I've tried to add a hasManyThrough relation to the Company model but that doesn't work either?
public function users()
{
return $this->hasManyThrough('App\User', 'App\Employee');
}
I think you're ring to get a list of coworkers for the current user and eager load the user and role?
$employee = Auth::user()->employee;
$companyEmployees = Company::with(['employees.user.roles')->find($employee->company_id);
Or perhaps:
$companyEmployees = Company::find($employee->company_id)->employees()->with('user.roles')->get();
$sorted = $companyEmployees->sortBy(function($employee){ return $employee->user->created_at; });
That might be a more direct route. Is your employee id in the user table or vice versa? The eloquent relationships are easy to set backwards.
Users::select('table_users.id')->with('roles')->join('table_employes', function($join) use ($employee) {
$join->on('table_employes.user_id','=','table_users.id')->where('table_employes.company_id', '=', $employee->company_id);
})->orderBy('tables_users.created_at')->get();
1. Create relationship for database table columns in migrtaion :
User Role
$table->foreign('user_id')->references('id')->on('users');
Users
$table->increments('id');
2. Create a model for each database table to define relationship
User.php (model)
public function userRoles()
{
return $this->hasOne('App\UserRoles', 'user_id', 'id');
}
Userroles.php (model)
public function user()
{
return $this->belongsTo('App\User', 'user_id', 'id');
}
3. Let controller handle database calls recommended to use REST api
Controller
use App\User;
use App\UserRoles;
class UserController extends Controller
{
public function index()
{
return User::with('userRoles')->orderBy('users.created_at', 'desc')->paginate(50);
}
}