php - Laravel 4 - How to get the data from the foreign table from each item in collection -
php - Laravel 4 - How to get the data from the foreign table from each item in collection -
i have standardoverall
table next columns:
sto_id | sto_transaction_id | sto_standard_id | sto_count | sto_total
and standard
table next columns:
std_standards_id | std_code | std_description | std_notes
sto_standard_id
in standardoverall
table foreign key std_standards_id
in standard
table.
i homecoming rows standardoverall
table belong sto_transaction_id
. how can homecoming info foreign table well, in 1 collection? code in controller:
$transactionid = session::get('transactionid'); $standardstats = standardoverall::whereid($transactionid)->get();
the standardoverall
model:
class standardoverall extends eloquent { /** * database table used model. * * @var string */ protected $table = 'sto_stat_overall'; protected $primarykey = 'sto_id'; public function standards() { homecoming $this->belongsto('standards'); } public function getstandards() { homecoming $this->hasone('standards', 'std_standards_id', 'sto_standard_id')->with('std_description'); } public function scopewhereid($query, $transactionid) { homecoming $query->where('sto_transaction_id', $transactionid); } }
so want count , total columns standards overall table , linked code , description standard table. possible?
you may seek this:
$transactionid = session::get('transactionid'); $standardstats = standardoverall::with(['standards' => function($query) { $query->select( 'std_standards_id', 'std_code', 'std_description', db::raw('count(*) count') )->groupby('std_standards_id'); }])->whereid($transactionid)->get();
php laravel laravel-4 relationship
Comments
Post a Comment