Reputation: 161
I have 3 models: User, Company and Branch.
In a blade file i want to be able to display the branch the company of the user belongs to.
In my opinion i should have the following relationships:
User -> Company : user belongsto a company and a company has many users , so this in a one-to-many relationship.
Company -> Branch: A company belongsto a branch and a branch can have many companies. So once again a one-to-many relationship.
I have foreign keys in the users table: company_id which references id on the company table. Another FK in the company table: branch_id which references id on the branch table.
In my blade file want to display the branch name like this: {{ $user->company->branch->name }} where only name is a property. Company and branch are relationships.
My query looks like this:
$users = User::with(['company','company.branche' => function($q){
$q->select('name');
}])->inRandomOrder()->paginate(24);
<?php
namespace App;
use App\Events\GeneralEvent;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Nicolaslopezj\Searchable\SearchableTrait;
use Spatie\Permission\Traits\HasRoles;
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable, SoftDeletes, HasRoles, SearchableTrait;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'gender', 'first_name','last_name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
...
public function company()
{
return $this->belongsTo(Company::class,'company_id');
}
}
<?php
namespace App;
use App\Events\GeneralEvent;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Nicolaslopezj\Searchable\SearchableTrait;
class Company extends Model
{
use SoftDeletes, SearchableTrait;
...
public function branche()
{
return $this->belongsTo(Branche::class);
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Nicolaslopezj\Searchable\SearchableTrait;
class Branche extends Model
{
protected $fillable = [
'name'
];
protected $searchable = [
'columns' => [
'name' => 10,
],
];
public function companies()
{
return $this->hasMany(Company::class);
}
}
However when i dump the $user->company i get null. So adding the branch after that is pointless for now. When i dump the user the relation shows up but is null. I have no idea where i am going wrong. Can someone please help?
Upvotes: 1
Views: 1317
Reputation: 161
So the problem was i had one of my foreign keys pointing to the wrong table. Doh.
Upvotes: 0
Reputation: 836
You have to include the id of the branch so that eloquent can link the branch and company you can do like that
$users = User::with(['company','company.branche' => function($q){
$q->select('id','name');
}])->inRandomOrder()->paginate(24);
OR Like that
$users = User::with('company.branche:id,name')->inRandomOrder()->paginate(24);
Upvotes: 5