为每个结果添加额外信息到laravel - > get()

时间:2016-04-08 14:16:33

标签: php laravel-5

我有'艺术家'表和'表演'表。性能表有一个性能时间字段,它是一个时间戳。

在回复我的艺术家数据时,我也会建立关系以获得表演:

Use "Class.URL"
Use "Class.JSON"

Sub Initialize
    '*** Local Notes classes used in agent
    Dim session As New NotesSession
    Dim db As NotesDatabase
    Dim thisdb As NotesDatabase
    Dim view As NotesView
    Dim col As NotesViewEntryCollection
    Dim entry As NotesViewEntry 
    Dim doc As NotesDocument
    '*** Custom classes
    Dim url As URLData
    Dim json As JSONData
    '*** Local variables to hold arguments passed from URL
    Dim startdate As String
    Dim enddate As String
    '*** Other local variables
    Dim jsontext As String 

    '*** Create new URLData object
    Set url = New URLData()
    '*** Create new JSONData object
    Set json = New JSONData()

    '*** Check start date and convert from ISO to US date format
    If url.IsValue("start") Then
        startdate = ISOtoUS(url.GetValue("start"))
    Else
        startdate = "01/01/1980"
    End If

    '*** Check end date and convert to US date format
    If url.IsValue("end") Then
        enddate = ISOtoUS(url.GetValue("end"))
    Else
        enddate = "12/31/2199"
    End If

    '*** Send MIME header to browser
    Print "content-type: application/json"
    jsontext = ""
    Set db = session.CurrentDatabase
    Set view = db.GetView("Events")
    Set col = view.AllEntries
    Set entry = col.GetFirstEntry()
    Do Until entry Is Nothing
        If CDat(entry.ColumnValues(0))>=CDat(startdate) Then
            If CDat(entry.ColumnValues(0))<=CDat(enddate) Then
                Call json.SetValue("id", CStr(entry.ColumnValues(5)))
                Call json.SetValue("title",CStr(entry.ColumnValues(3)))
                Call json.SetValue("start", Format$(CDat(entry.ColumnValues(7)),"mm/dd/yyyy hh:nn ampm"))
                Call json.SetValue("end", Format$(entry.ColumnValues(8),"mm/dd/yyyy hh:nn ampm"))
                Call json.SetValue("className","eventData eventData" + CStr(entry.ColumnValues(6)))
                Call json.NoStatus()
                '*** Make the entry editable (change date/time) 
                Call json.SetBoolean("editable", True)
            End If
        End If
        jsontext = jsontext + json.GetJSON() + "," + Chr$(13)
        Set entry = col.GetNextEntry(entry)
    Loop
    If Len(jsontext)>4 Then
        jsontext = Left$(jsontext,Len(jsontext)-2)
    End If
    Print "[ " + jsontext + " ]"
End Sub


%REM
    Function ISOtoUS
    Description: Convert ISO date to US date,
    e.g 2015-05-08 to 05/08/2015
%END REM
Function ISOtoUS(dateISO As String) As String
    Dim tmp As String
    ' 2015-02-03
    tmp = Mid$(dateISO,6,2) + "/"
    tmp = tmp + Mid$(dateISO,9,2) + "/"
    tmp = tmp + Left$(dateISO,4)
    ISOtoUS = tmp 
End Function

我现在要做的是为演出关系添加另一部分信息,这将显示使用演奏时间字段的演出时间。

我在其他地方使用不同格式的字段数据,所以不想使用mutator。

谢谢!

2 个答案:

答案 0 :(得分:1)

你仍然可以使用mutator。我们假设您的Performance模型有一个time字段。

public function getModifiedTimeAttribute() {
    return $this->time . ' and something else!';
}
您要$performance->modified_time访问的

。默认情况下,此模型字段不会存在于模型的Laravel阵列/ JSON输出中,但您可以将其添加到模型中以使其显示在那里:

protected $appends = ['modified_time'];

答案 1 :(得分:0)

您可以在Performance模型中使用动态属性,如下所示:

class Performance extends Model
{
    [...]

    public function getTimeToNextAttribute()
    {
        //calculate Time to next Performance and return it
    }
}

然后,您可以通过$ performance-&gt; time_to_next访问此媒体资源。