Not able to create eloquent relationship with table which have custom primary key - laravel

When i'm trying to get user details from company Model i'm getting NULL value.
class User extends Model
{
protected $primaryKey = 'uid';
}
class Company extends Model
{
$table = 'company';
public function users(){
return $this->hasMany('App\User','user_id','uid');
}
}
class RepoController{
public function index(){
$user = Company::where('id',1)->with('user')->get();
/// Returning value but without Users
}
}`

users table should contain company_id and you should define id as company ID because company table doesn't use custom ID:
public function users()
{
return $this->hasMany('App\User', 'company_id', 'id');
}
Or just this:
public function users()
{
return $this->hasMany('App\User');
}
Also, you're trying to load user relationship, but it should be users(), so do this instead:
Company::where('id', 1)->with('users')->get();

I think your ids are in wrong position
try swapping them like
return $this->hasMany('App\User','uid','user_id');

Related

Relationship through one model

I have three tables - users, properties, devices.
users
id
name
properties
id
name
user_id
devices
id
name
property_id
How to define straight relationship to user model from devices model?
class Device extends Model
{
public function user()
{
return $this->....();
}
}
Is it possible to define such relation? Thanks.
Make sure to set all relation in other classes.
class Property extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
class Device extends Model
{
public function property()
{
return $this->belongsTo('App\Property');
}
public function user()
{
return $this->belongsTo('App\User', null, null, 'property');
}
}
You can provide a relation with in 4th parameter of belongsTo method.
//user model
public function role()
{
return $this->belongsTo('App\Role');
}
public function employee()
{
return $this->hasMany('App\Employee');
}
//role model
public function users()
{
return $this->hasMany('App\User');
}
//employee model
public function users()
{
return $this->belongsTo('App\User');
}
//value show using tinker command in command promote
App\User::find(1)->employee;
App\User::find(1)->role;
I think I have found the solution, this is what I end up with.
class Device extends Model
{
public function user()
{
$instance = new User();
$instance->setTable('properties');
$query = $instance->newQuery();
return (new BelongsTo($query, $this, 'property_id', $instance->getKeyName(), 'property'))
->join('users', 'users.id', '=', 'properties.user_id')
->select(DB::raw('users.*')
);
}
}
Hope this will be of help for somebody.

Get resources with hasManyThrough a model using BelongsTo

I have the following models:
User:
- id
- name
Location:
- id
- name
- region_id
table: user_location
- user_id
_ location_id
The user belongsToMany location through that table. I also have another model:
Region
- id
- name
I defined Region hasMany Locations.
With those relationships, how do I define a relationship between User and Region which Region will be able to find all users under all Locations associated with it?
<?php
class User extends Model
{
public function locations() {
return $this->belongsToMany('App\Location', 'user_location');
}
}
class Location extends Model
{
public function users() {
return $this->belongsToMany('App\User', 'user_location');
}
public function region() {
return $this->belongsTo('App\Region', 'region_id');
}
}
class Region extends Model
{
public function locations() {
return $this->hasMany('App\Location', 'region_id');
}
public function users() {
// what am I supposed to put in here?
}
}
There is no native relationship for this case.
I created a HasManyThrough relationship for situations like this: Repository on GitHub
After the installation, you can use it like this:
class Region extends Model {
use \Staudenmeir\EloquentHasManyDeep\HasRelationships;
public function users() {
return $this->hasManyDeep(User::class, [Location::class, 'user_location']);
}
}

How to access third table data from eloquent relation between two tables in laravel?

i am sorry, if my question not make sense...
i have these two tables Area, and Address and are related via foreign key... here is the area and address models
class Area extends Model
{
protected $table='areas';
public function sections(){
$this->hasMany(Area::class,'area','id');
}
public function address(){
$this->belongsTo(Address::class,'id');
}
}
class Address extends Model
{
protected $table='address';
public function area(){
return $this->hasMany(Area::class,'id');
}
}
and i have a third table section which is connected to area table.
my question can i make eloquent relation that can access the data of address table from the section "section table connect with area not address"
class Section extends Model
{
protected $table ='sections';
protected $fillable =[
'sec_code',
'area',
'id',
];
public function beneficiaries(){
return $this->hasMany(Beneficiary::class,'ben_sec','id');
}
public function area_sec(){
return $this->belongsTo(Area::class,'area');
}
public function address(){
// need the eloquent relation if could
}
}
thank you
Call it as a property not a method:
$model->area_sec;
You can get the data with access it from your Area relation first, so the code will be like this:
class Section extends Model
{
protected $table ='sections';
protected $fillable =[
'sec_code',
'area',
'id',
];
public function area_sec(){
return $this->belongsTo(Area::class,'area');
}
public function address(){
return $this->area_sec->address;
}
}

Is it possible to insert record through chain foreign key relationship in Laravel 5.2

I have three tables/models :user,employee and employee_disability.
employee has foreign key user_id.
employee_disability contain emp_id foreign key.
Is there a way to perform the following action:
$user->employee()->empdisability()->create(['name'=>$name,'percent'=>$percent]);
Any help is appreciated. Thank You in advance.
MODELS:
class user extends Authenticatable{
public function employees(){
return $this->hasMany('App\Models\employee','user_id');
}
}
class employee extends Model{
public function user(){
return $this->belongsTo('App\Models\user','user_id');
}
public function empdisability(){
return $this->hasMany('App\Models\emp_disability','emp_id');
}
}
class emp_disability extends Model{
public function employee(){
return $this->belongsTo('App\Models\employee','emp_id');
}
}
CONTROLLER
public function storeDisability(Request $request){
$user=Auth::user();
$name=$request->name; $percent=$request->percent;
$user->employee()->empdisability()->create(['name'=>$name,'percent'=>$percent]);
}
$user->employee() returns the query builder for the employee relation, but what you need is the instance.
This should work, notice the lack of parantheses on employee
$user->employee->empdisability()->create(['name'=>$name,'percent'=>$percent]);

Laravel belongsTo and hasMany relationship on same model

How do you apply belongsTo and hasMany relationship on same model?
For example, I have one User and one Project model. Now Project model has method user() and User model has projects() method.
Now I want a user to share projects with other users. So that I can have methods like users() in Project model and shared_projects() in User model.
How I can achieve that?
Here is my current Project model
class Project extends \Eloquent {
protected $fillable = [];
public function user() {
return $this->belongsTo('User');
}
}
And this my User model
class Project extends \Eloquent {
protected $fillable = [];
public function projects() {
return $this->hasMany('Project');
}
}
In this case, User and Project have Many-to-Many relationship because one user has many projects and one project belongs to many user. Therefore, you should create a third table (project_user) which connect User table and Project table in the database.
And you set Many-to-Many relation to User Model and Project Model.
class User extends Eloquent {
public function projects()
{
return $this->belongsToMany('Project');
}
}
In Project model
class Project extends Eloquent {
public function users()
{
return $this->belongsToMany('User');
}
}
Then you can do something like this
//users in the same project
$users = Project::find($id)->users;
Okay, so here it is how I solved it. I used pivot table as you suggested but I even added hasMany relationship like this way. I am still not sure what I am doing is perfectly correct but it worked. :)
class Project extends \Eloquent {
protected $fillable = [];
public function users() {
return $this->belongsToMany('User');
}
public function user() {
return $this->belongsTo('User');
}
}
And here is the key ingredient in User model
class User extends \Eloquent {
protected $fillable = [];
public function projects_owned() {
return $this->hasMany('Project', 'user_id');
}
public function projects() {
return $this->belongsToMany('Project');
}
}

Resources