[英]how to select field from other table laravel
i want to select a field from another table, how i can do it in laravel, im still newby in laravel. 我想从另一个表中选择一个字段,我该如何在laravel中做到这一点,即时通讯仍然在laravel中。
here my code 这是我的代码
$company = Company::select(
['companies.id', 'companies.CompanyName', 'companies.Discount', 'companies.OrgNumber', 'companies.ExternalID', 'companies.DCity'])
->join('companystructures AS cs', 'cs.ChildCompanyID', '=', 'companies.id')
->where(['companies.Active' => 1, 'cs.Active' => 1])->get();
i want to add more select to field ParentCompanyID
from table companystructures
. 我想从表companystructures
向字段ParentCompanyID
添加更多选择。
i try like this but got error 我尝试这样但出现错误
$company = Company::select(['companies.id', 'companies.CompanyName', 'companies.Discount', 'companies.OrgNumber','companies.ExternalID', 'companies.DCity', '**companystructures.ParentCompanyID**'])
->join('companystructures AS cs', 'cs.ChildCompanyID', '=', 'companies.id')
->where(['companies.Active' => 1, 'cs.Active' => 1])->get();
Remove **
and use cs.ParentCompanyID
instead of companystructures.ParentCompanyID
in select
删除**
并在select
使用cs.ParentCompanyID
而不是companystructures.ParentCompanyID
$company = Company::select([
'companies.id',
'companies.CompanyName',
'companies.Discount',
'companies.OrgNumber',
'companies.ExternalID',
'companies.DCity',
'cs.ParentCompanyID'
])
->join('companystructures AS cs', 'cs.ChildCompanyID', '=', 'companies.id')
->where(['companies.Active' => 1, 'cs.Active' => 1])
->get();
You shouldn't do it like this. 你不应该这样。 There is an elegant way to do this using Eloquent Relationships on your Model 有一种优雅的方法可以在模型上使用雄辩性关系
First we will define a hasMany relationship in the Company Model like this. 首先,我们将像这样在公司模型中定义hasMany关系。
public function structures()
{
return $this->hasMany(CompanyStructures::class, foreign_key, local_key);
}
Now when we try to query the "Company" model. 现在,当我们尝试查询“公司”模型时。 We can do it like this 我们可以这样
return Company::with('structures')->get([ALL_YOUR_SELECT_COLUMNS]);
This will return all the columns defined in the ->get() with all the columns matching in the structures table with primary key of "Company" model. 这将返回-> get()中定义的所有列,并以“ Company”模型的主键在结构表中匹配所有列。
You can restrict the data with adding a closure to the ->with() pipe and get only the columns that you need. 您可以通过在-> with()管道中添加闭包来限制数据,并仅获取所需的列。 For example if I want to get only the "company structures location" property(I am assuming you would be having one), then we would do something like this. 例如,如果我只想获得“公司结构位置”属性(我假设您将拥有一个),那么我们将做类似的事情。
return Company::with(['structures' => function(){
return $query->select(columns_from_child_table);
}])->get([ALL_YOUR_SELECT_COLUMNS]);
I hope that solves your answer. 希望能解决您的答案。 I seriously suggest you read the documentation from the link I provided above and make sure you use laravel eloquent as much as possible. 我强烈建议您阅读上面提供的链接中的文档,并确保尽可能多地使用laravel。 Refer the document version according to your laravel version. 请根据您的laravel版本参考文档版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.