[英]Access to all information of tables from pivot table Laravel
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?公司,分支机构,部门,工作,雇员
'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 当我只查询一个注册我想使用withPivot()时我想访问所有信息,但是我不知道需要做什么,我已经在Model.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 这给了我第一家公司的信息以及功能getAllInformation中的信息,但是我想要所有这些
Before i have this in the Model CompaniesBranchesDepartmentsJobsEmployes.php 在我进入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 这给了我所有信息的Json并且很好,但是我想使用数据透视函数,因为这是laravel用于表数据透视的正确方法,我需要为var $ items或所有其他信息中的所有信息制作一个数组帮助谢谢:D
You can define $with array in you model. 您可以在模型中定义$ with数组。
Example 例
class Company extends Model {
$with = [
'company',
'branch',
...etc
];
}
And than just call 而不仅仅是打电话
Company:limit(1)->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.