简体   繁体   English

Laravel所属的ToMany()日期描述

[英]Laravel belongsToMany() date desc

I use a belongsToMany() in Laravel, but a staff member can have more than one position, I want to bring the staff with the largest date and desc id. 我在Laravel中使用了一个EmiratesToMany(),但是一个职员可以担任多个职位,我想带最多日期和desc id的职员。

PHP代码/ MySQL结果

In your Person.php , add a back relation to Position.php 在您的Person.php中 ,向Position.php添加一个反向关系

public function positions()
{
  return $this->belongsToMany('Path\To\Person')->withPivot(['id', 'person_id', 'position_id', 'created_at', 'updated_at']);
}

Now you can do something like.... 现在您可以做类似...

$positions = Person::positions()
                   ->wherePivot('action_date', '>=', $date)
                   ->orderBy('person_position.id', 'desc')
                   ->get();

$pivotTable = $positions->map(function($position) {
                return $position->pivot;
              });

Let me know if you face any other issue :) 让我知道您是否还有其他问题:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM