Php Laravel 4-如何从集合中的每个项目的外部表中获取数据
我有一个Php Laravel 4-如何从集合中的每个项目的外部表中获取数据,php,laravel,laravel-4,relationship,Php,Laravel,Laravel 4,Relationship,我有一个StandardOverall表,表中有以下列: sto_id | sto_transaction_id | sto_standard_id | sto_count | sto_total std_standards_id | std_code | std_description | std_notes 以及带有以下列的标准表格: sto_id | sto_transaction_id | sto_standard_id | sto_count | sto_total std_st
StandardOverall
表,表中有以下列:
sto_id | sto_transaction_id | sto_standard_id | sto_count | sto_total
std_standards_id | std_code | std_description | std_notes
以及带有以下列的标准表格:
sto_id | sto_transaction_id | sto_standard_id | sto_count | sto_total
std_standards_id | std_code | std_description | std_notes
StandardOverall
表中的stou-standard\u-id
是standard
表中std\u-standards\u-id
的外键
我返回standardtowall
表中属于stou事务\u id
的所有行。如何在一个集合中从外部表返回数据?此代码位于控制器中:
$transactionID = Session::get('transactionID');
$standardStats = StandardOverall::whereID($transactionID)->get();
标准总体
模型:
class StandardOverall extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'sto_stat_overall';
protected $primaryKey = 'sto_id';
public function Standards() {
return $this->belongsTo('Standards');
}
public function getStandards() {
return $this->hasOne('Standards', 'std_standards_id', 'sto_standard_id')->with('std_description');
}
public function scopewhereID($query, $transactionID) {
return $query->where('sto_transaction_id', $transactionID);
}
}
因此,我需要标准总体表中的count和total列,以及标准表中的链接代码和描述。这可能吗?您可以尝试以下方法:
$transactionID = Session::get('transactionID');
$standardStats = StandardOverall::with(['standards' => function($query) {
$query->select(
'std_standards_id', 'std_code', 'std_description', DB::raw('count(*) as count')
)->groupBy('std_standards_id');
}])->whereID($transactionID)->get();