我有一个日期,我需要拆分一些组件.例如
let components = NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitHour
let date = calendar.components(components, fromDate: aDate, toDate: NSDate(), options: nil)
var dateToPrint = "\(date.day) days \(date.hour) hours"
Run Code Online (Sandbox Code Playgroud)
dateToPrint将是从aDate到现在的天数和小时数.但如果我想要几周而不是几天
let components = NSCalendarUnit.CalendarUnitWeek | NSCalendarUnit.CalendarUnitHour
let date = calendar.components(components, fromDate: aDate, toDate: NSDate(), options: nil)
var dateToPrint = "\(date.week) weeks \(date.hour) hours"
Run Code Online (Sandbox Code Playgroud)
date.week不存在.那我怎么解决这个问题呢?
我有三个模型Bill,Product和Process。Bill有一个 ManyToMany 关系,Product数据透视表有一些额外的字段。我编写Bill模型类如下:
<?php
class Bill extends Model
{
function products(){
return $this->belongsToMany(\App\Product::class)
->withPivot('process_id') // the id of the Process
->withTimestamps();
}
}
Run Code Online (Sandbox Code Playgroud)
该Process模型是一个带有id和 的简单表格name。我正在关联数据透视表中的 id 以参考流程,名称可能会随着时间的推移而改变,但仍然引用相同的概念,因此我无法关联名称。
单个的显示页面Bill列出了在表格中关联的产品,如下所示:
@foreach($bill->products as $product)
<tr>
<td>{{$product->barcode}}</td>
<td>{{$product->pivot->process_id}}</td>
</tr>
@endforeach
Run Code Online (Sandbox Code Playgroud)
所以问题是我需要进程的名称,但我有 id。我不知道我怎么能得到这个名字。
谢谢