I have two database tables called deliverables, and deliverable_version.
Deliverable hasMany Deliverable_Version.
Deliverable has id column, and the model has a function to get the most recent version:
public function getRecentVersion()
{
$deliverable_version_max = $this->versions()->max('version');
$deliverable_version = DeliverableVersion::where([
'version'=>$deliverable_version_max,
'deliverable_ID'=>$this->id,
])->first();
return $deliverable_version;
}
Deliverable_version table has id, deliverable_ID, title, message, deadline, version, created_at, updated_at.
How I would perform an operation where I can:
Get all deliverables and the most recent version associated with the deliverable.
Sort the deliverables by it by the columns of the deliverable_version table (updated_at)
Paginate the result?
Here is my attempt at it, which isn't working because it selects all the versions when I join the tables, not just the recent version:
$deliverables = Accounts::find($owner_account_ID)->deliverables();
$deliverables = $deliverables->join('deliverable_version', 'deliverable.id', '=', 'deliverable_version.deliverable_ID');
$deliverables = $deliverables->search($search);
$deliverables = $deliverables->orderBy('deliverable_version.updated_at', 'desc');
$deliverables = $deliverables->paginate(4);
Any help is appreciated. Thanks.
in Accounts module add:
public function deliverable_version(){
return $this->hasMany('App\deliverable_version');
}
in controller:
enter code here
$deliverables = Accounts::find($owner_account_ID)->deliverables()>with('deliverable_version');
foreach($deliverables as $deliverable){
// last version
$deliverable = $deliverables->deliverable_version->last();
}
in Accounts module add:
public function deliverable_version(){
return $this->hasMany('App\deliverable_version')->orderBy('update_at');
}
in controller:
$deliverables = Accounts::find($owner_account_ID)->deliverables()>with('deliverable_version');
foreach($deliverables as $deliverable){
// last version
$deliverable = $deliverables->deliverable_version->last();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.