Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Delarates the reverse relation on your other model, I has this. Supose I have Departments and Employees than my modeles Department and Employee, so you have declares the belongsToMany on the two models as this,

Department Model:

<?php

namespace Tlc;

use Illuminate\Database\Eloquent\Model;

class Department extends Model


{

protected $table = 'departments';

 protected $fillable= ['name_department', 'description_department'];

public function employees()

{
	return $this->belongsToMany('Tlc\Employee');
}

 public function user()

{
    return $this->hasManyThrough('App\Employee', 'App\User');
}

}

Employee Model:

<?php
namespace Tlc;
use Illuminate\Database\Eloquent\Model;
class Employee extends Model
{
protected $table = 'employees';
protected $fillable= ['name', 'lastname','job','email', 'department_id'];
public function departments()
{
	return $this->belongsToMany('Tlc\Department');
}
public function user()
{
	return $this->hasOne('Tlc\User');
}
}
Last updated 9 years ago.
0

I know how to connect. The question is how to get list of items.

With your example say you know that ID of the department is 1. How do you get list of employees of department_id = 1using Employee model? I know that I can do it using Department model like this.

$employee = Department::find(1)->employees;

But how to get the same result with Employee model?

0

the doc mention has many through relationship for exactly what you are trying to do e.g get users for department when they are not directly linked..

Q. can a user be in more than one department? if NO then why not put department_id in users table?

0

shez1983 said:

the doc mention has many through relationship for exactly what you are trying to do e.g get users for department when they are not directly linked..

I read documentation. Please give me an example. Because i could not find solution in docs. May be I was looking not so hard.

shez1983 said:

Q. can a user be in more than one department? if NO then why not put department_id in users table?

This is May to Many relation. Of course user can be in more than one department. This is the case of the question.

0

since your wording was/is confusing... if its many to many then create such relationship? but you also said you cannot access users without going through user_groups... if it was many_many i would have expected a table called user_departments or department_users etc.. but you clearly state that users can only be retrieved as there is no associated table between users & departments.. and what u were referring to was something lke this (excuse the diagram)

Department ---- User_roles ---- Users.. and user_role didnt seem to me as a many_many pivot table..

0

Try:

$employee = Department::with('employees')->get();

or

$employee = Employee::with('departments')->get();

or

$employee= Employee->departments->find(1);
Last updated 9 years ago.
0

shez1983 said:

the doc mention has many through relationship for exactly what you are trying to do e.g get users for department when they are not directly linked..

Q. can a user be in more than one department? if NO then why not put department_id in users table?

The hasmanyThrough, just work on relation hasOne o hasMany no with pivot table on the belongsToMany

0

Try this and helpme too because i have that question too. jaja,

Suposed have my example:

$employees = Tlc\Employee::with('departments.name_department')->get();
0

With

$employees = Tlc\Employee::with('departments')->get;

I get this, how pass to the view for {{name}} {{lastname}} {{name_department}} Thanks.

Collection {#329 ▼
#items: array:11 [▼
0 => Employee {#314 ▶}
1 => Employee {#315 ▼
  #table: "employees"
  #fillable: array:5 [▶]
  #connection: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:7 [▶]
  #original: array:7 [▶]
  #relations: array:1 [▼
    "departments" => Collection {#332 ▼
      #items: array:1 [▼
        0 => Department {#331 ▼
          #table: "departments"
          #fillable: array:2 [▶]
          #connection: null
          #primaryKey: "id"
          #perPage: 15
          +incrementing: true
          +timestamps: true
          #attributes: array:5 [▼
            "id" => "1"
            "name_department" => "Compras"
            "description_department" => "Lorem Compras"
            "created_at" => "2015-10-13 04:49:16"
            "updated_at" => "2015-10-13 04:49:16"
          ]
          #original: array:7 [▶]
          #relations: array:1 [▶]
          #hidden: []
          #visible: []
          #appends: []
          #guarded: array:1 [▶]
          #dates: []
          #dateFormat: null
          #casts: []
          #touches: []
          #observables: []
          #with: []
          #morphClass: null
          +exists: true
          +wasRecentlyCreated: false
        }
0

shez1983 said:

since your wording was/is confusing... if its many to many then create such relationship? but you also said you cannot access users without going through user_groups... if it was many_many i would have expected a table called user_departments or department_users etc.. but you clearly state that users can only be retrieved as there is no associated table between users & departments.. and what u were referring to was something lke this (excuse the diagram)

I am sorry for not very clear English. It is not that I cannot set up relations. i can do that on both sides. I cannot call User model. This is how I get list of all roles of user ID 1 by calling User model.

$roles = User::find(1)->roles;

But how to get the same result, list of all roles of user ID 1 calling Role model? Starting like

$roles = Role::.....
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.