I hope somebody help with this, regards, i have a pivot table what have this fields what is only ids of the other tables Companies, Branchs, Departments, Jobs, Employes
'id',
'company_id',
'branch_id',
'department_id',
'job_id',
'employee_id'
I want to access to all information when i consult one only register i want to use withPivot() but i dont understand what need to do i already have this in model Company.php
public function getAllInformation()
{
return $this->belongsToMany(Company::class, 'companies_branches_departments_jobs_employes', 'id', 'company_id');
}
And in the controller i have this
public function index()
{
$items = Company::with('getAllInformation')->limit(1)->get();
$information =
[
'items' => $items,
'total' => count($items)
];
return $information;
}
This give me the information of the first Company with the information in the function getAllInformation but i want all of the all
Before i have this in the Model CompaniesBranchesDepartmentsJobsEmployes.php
public function company()
{
return $this->hasOne(Company::class, 'id', 'company_id');
}
public function branch()
{
return $this->hasOne(Branch::class, 'id', 'branch_id');
}
public function department()
{
return $this->hasOne(Department::class, 'id', 'department_id');
}
public function job()
{
return $this->hasOne(Job::class, 'id', 'job_id');
}
public function employee()
{
return $this->hasOne(Employe::class, 'id', 'employee_id');
}
And in the Controller this
public function getAllInformation()
{
$items = CompaniesBranchesDepartmentsJobsEmployes::with('company', 'branch', 'department', 'job', 'employee')->limit(10)->get();
$inventory =
[
'items' => $items,
'total' => count($items)
];
return $inventory;
}
This give me a Json of all information and is fine but i want to use pivot functions because is the right way of laravel for work with table pivot, i need to make a array for all the information in the var $items or what, any help thanks :D
You can define $with array in you model.
Example
class Company extends Model {
$with = [
'company',
'branch',
...etc
];
}
And than just call
Company:limit(1)->get();