laravel returning relationships with eloquent and laravel - php

In my database I am an organisations table this table has the following relationships,
Many-to-many with users
Many-to-many with clients
One-to-many with projects
In turn these relationships have other relationships for example projects
One-to-one with client
In my controller I am doing the following,
$organisation = Organisation::all();
$organisation->load('users');
$organisation->load('clients');
$organisation->load('teams');
$organisation->load('projects');
return Response::json($organisation, 200);
So get all the organisations and there relational data.
However what I wanting to do is also get the relationships of relationships, so for example get the client that is related to each project that an organisation has? I thought that doing what I am doing would have worked but obviously not.
Here are my models,
Organisation,
class Organisation extends Eloquent {
//Organsiation __has_many__ users (members)
public function users()
{
return $this->belongsToMany('User')->withPivot('is_admin');
}
//Organisation __has_many__ clients
public function clients()
{
return $this->belongsToMany('Client');
}
//Organisation __has_many__ projects
public function projects()
{
return $this->belongsToMany('Project');
}
}
Projects
class Project extends Eloquent {
protected $fillable = [
'name',
'description',
'total_cost',
'start_date',
'finish_date',
'sales_person',
'project_manager',
'client_id',
'organisation_id',
'user_id'
];
public function organisations()
{
return $this->belongsToMany('Organisation');
}
public function salesperson() {
return $this->belongsTo('User', 'sales_person');
}
public function clients() {
return $this->belongsTo('Client', 'client_id');
}
}
Clients
class Client extends Eloquent {
public function organisations()
{
return $this->belongsToMany('Organisation');
}
public function users()
{
return $this->belongsToMany('User');
}
public function projects()
{
return $this->hasMany('Project');
}
}

Have you tried:
$organisations = Organisation::with('projects', 'projects.clients')->all();
foreach($organisations as $organisation) {
foreach($organisation->projects as $project) {
foreach($project->clients as $client) {
echo $client->name;
}
}
}

Related

How can I resolve two foreign key and three table relation with eloquent

I can't resolve this relation with eloquent. I don't know, maybe it is illegal relation. But I can't think of another relationship. I need one CustomerCodeReferences row from SaleProduct row.
Why not:
class CustomerCodeReference extends Model
{
public function saleProducts()
{
return $this->hasMany(\App\Models\SaleProduct::class, 'product_id', 'product_id');
}
public function documents()
{
return $this->hasMany(\App\Models\Document::class, 'customer_id', 'customer_id');
}
public function scopeCustomer($query, int $customer)
{
$query->where('customer_id', $customer);
}
}
class Document extends Model
{
public function saleProducts()
{
return $this->hasMany(\App\Models\SaleProduct::class);
}
public function customerCodeReferences()
{
return $this->hasMany(\App\Models\CustomerCodeReference::class, 'customer_id', 'customer_id');
}
}
class SaleProduct extends Model
{
public function customerCodeReferences()
{
return $this->hasMany(\App\Models\CustomerCodeReference::class, 'product_id', 'product_id');
}
public function documents()
{
return $this->belongsTo(\App\Models\Document::class);
}
}
The Project's CustomerCodeReferences can be accessed like this:
$project->customerCodeReference()->customer(1)->get();

How to use Multiple relationship in laravel 5.4

In my app i have define relationship (profile, user, level) but when I fetch data it is showing an error (Trying to get property 'email' of non-object) how can i solve this thank in advance.
this is User Model
public function profile()
{
return $this->hasOne(Profile::class, 'user_id');
}
Profile Model
public function user()
{
return $this->belongsTo(User::class, 'id');
}
public function level()
{
return $this->belongsTo(Level::class, 'id');
}
Level Model
public function profile()
{
return $this->hasOne(Profile::class, 'level_id');
}
This is Controller ProfileController
$users = Profile::with(['user', 'level'])->where('is_bd_partner', 'Yes')->get();
foreach ($users as $key => $value)
{
echo $value->first_name.'<br>';
echo $value->last_name.'<br>';
echo $value->user->email.'<br>';
echo $value->level->level.'<br>';
}
Note that belongsTo takes foreign_key as the first parameter.
So you should change the Profile Model as
public function user()
{
return $this->belongsTo(User::class, 'user_id');
}
public function level()
{
return $this->belongsTo(Level::class, 'level_id');
}
Read more here

Laravel Polymorphic Relations returns NULL

I've read many posts about this issue but none of them works for me. I have a 'ISA' relationship in my database. A person can be either a Patient or a Nurse:
class Person extends Model
{
protected $table = 'persons';
public function commentable()
{
return $this->morphTo();
}
}
class Patient extends Model
{
public function persons()
{
return $this->morphMany('App\Person', 'commentable');
}
}
class Nurse extends Model
{
public function persons()
{
return $this->morphMany('App\Person', 'commentable');
}
}
This is my tables and the data inside them:
And this is my Route:
Route::get('person', function () {
$person = Person::find(1)->commentable();
return json_decode(json_encode($person), true);
});
I get an empty array!
You have to access the relationship as a property:
$person = Person::find(1)->commentable;

Eloquent model relationship for intermediate table

Consider the following table structure:
user table
id
name
lang_region_id
lang_region table
id
lang_id
region_id
lang table
id
name
region table
id
name
Fairly new to the Laravel framework, but trying to setup Eloquent models and relationships to an existing database. I want to establish the relationship between my user model and the lang and region models. The lang_region table defines what language and region combinations are available and then we can link each user to a valid combination.
I have read through the Laravel documentation several times looking for the proper relationship type, but is seems that the Many to Many and Has Many Through relationships are close, but since our user.id isn't used in the intermediate table I may be out of luck.
Sorry for the amateur question, but just getting used to Laravel and ORMs in general.
I would use the lang_region table as both a pivot table and a regular table with its own model.
class LangRegion extends model
{
protected $table = 'lang_region';
public function language()
{
return $this->belongsTo(Language::class, 'lang_id');
}
public function region()
{
return $this->belongsTo(Region::class);
}
public function users()
{
return $this->hasMany(User::class);
}
}
class User extends model
{
protected $table = 'user';
public function langRegion()
{
return $this->belongsTo(LangRegion::class);
}
}
class Language extends model
{
protected $table = 'lang';
public function regions()
{
$this->belongsToMany(Region::class, 'lang_region', 'lang_id', 'region_id');
}
public function users()
{
$this->hasManyThrough(User::class, LangRegion::class, 'lang_id', 'lang_region_id');
}
}
class Region extends model
{
protected $table = 'region';
public function languages()
{
$this->belongsToMany(Language::class, 'lang_region', 'region_id', 'lang_id');
}
public function users()
{
$this->hasManyThrough(User::class, LangRegion::class, 'region_id', 'lang_region_id');
}
}
If I understand what you want correctly:
class User extends Model {
private function lang_region() {
return $this->hasOne(LangRegion::class)
}
public function lang() {
return $this->lang_region()->lang();
}
public function region() {
return $this->lang_region()->region();
}
}
class LangRegion extends Model {
public function lang() {
return $this->belongsTo(Lang::class);
}
public function region() {
return $this->belongsTo(Region::class);
}
}

Laravel + Eloquent : combine 2 tables in one query

I am trying to perform a query with Eloquent where I can get all the courses of an user.
Users have their courses because they are in some grade. So all courses of this grade will be included.
$user->grade->courses
Now I'd like to include some courses that are not in the user's grade thanks to the pivot table : users_courses. But I don't know how to query all tables at once using Eloquent.
EDIT : Here is what have
class Users extends Eloquent {
public function courses()
{
return $this->belongsToMany('Courses', 'users_courses', 'student_id', 'course_id');
}
public function grade()
{
return $this->belongsTo('Grades', 'grade_id');
}
}
class Courses extends Eloquent {
public function grade()
{
return $this->belongsTo('Grades');
}
public function users()
{
return $this->belongsToMany('Users', 'users_courses', 'student_id', 'course_id');
}
}
class Grades extends Eloquent {
public function courses()
{
return $this->hasMany('Courses', 'grade_id');
}
public function users()
{
return $this->hasMany('Users', 'grade_id');
}
}

Categories