将数据循环到json中


Loop data into json?

我想循环每个约会并将其放入json中,像这样:

[
   {
      "id":"1",
      "title":"Test 1",
      "start":"2016-09-16 12:00:00",
      "end":"2016-09-16 14:00:00",
      "allDay":false
   },
   {
       "id":"2",
       "title":"Test 2",
       "start":"2016-09-15 12:00:00",
       "end":"2016-09-15 14:00:00",
       "allDay":false
   }
]   

数据库中有一个表appointments

id | title  | appointment_date_start | appointment_date_end |
-------------------------------------------------------------
1  | Test 1 | 2016-09-16 12:00:00    | 2016-09-16 14:00:00  |
2  | Test 2 | 2016-09-15 12:00:00    | 2016-09-15 14:00:00  |

在我的feed.blade.php中,我得到了所有的数据。所以我可以这样做:

@foreach ($appointments as $appointments)
    {{ $appointment->id }}
    {{ $appointment->title }}
    {{ $appointment->appointment_date_start }}
    {{ $appointment->appointment_date_end }}
@endforeach

从数据库输出数据。但是如何在数组中查找它呢?

$event_array[] = array(
    'id' => {{ $appointment->id }},
    'title' => {{ $appointment->gender }},
    'start' => {{ $appointment->appointment_date_start }},
    'end' => {{ $appointment->appointment_date_end }},
    'allDay' => false
);

编辑

我在我的公共函数中尝试了这个

public function feed()
{   
    $feed = Appointment::all();
    return $feed->toJson();
    return view('appointments/feed');
}

这是我得到的

{
    id: 35,
    user_id: 1,
    gender: "men",
    appointment_date_start: "2016-09-14 20:00:00",
    appointment_date_end: "2016-09-14 20:40:00",
    created_at: "2016-09-16 08:16:16",
    updated_at: "2016-09-16 08:16:16"
}

我需要删除gender, created_at and updated_at和任命ment_date_start应该开始和任命ment_date_end应该结束?这可能吗?

如果要删除created_at和updated_at,只需在从数据库获取数据时不选择属性:

$feed = Appointment::select('id', 'user_id', 'appointment_date_start as start', 'appointment_date_end as end')->get(); 
// leave created_at and updated_at
// select('old as new') to change the attribute name

然后用json_encode

将数组编码为json
return json_encode($feed);

你可以直接点击{{ $appointments->toJson() }},这是你想要的吗?