我有一个StandardOverall
表,其中包含以下列:
sto_id | sto_transaction_id | sto_standard_id | sto_count | sto_total
以及包含以下列的Standard
表:
std_standards_id | std_code | std_description | std_notes
sto_standard_id
表中的 StandardOverall
是std_standards_id
表中Standard
的外键。
我返回StandardOverall
表中属于sto_transaction_id
的所有行。如何从外表中返回数据,所有这些都在一个集合中?此代码位于控制器中:
$transactionID = Session::get('transactionID');
$standardStats = StandardOverall::whereID($transactionID)->get();
StandardOverall
模型:
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列以及标准表中的链接代码和描述。这可能吗?
答案 0 :(得分:0)
你可以试试这个:
$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();