Doing a small CRUD-App and I'm having problems creating an instance, on Api endpoint: 'event/' the Controller named: EventController and Method: create is called.
This is what the method looks like:
public function create(Request $request) {
$request->validate([
'title' => 'required|max:30',
'description' => 'required|max:455',
'place' => 'required',
'time_happening' => 'required|date',
]);
$event = Event::createFromArray($request->toArray());
return response()->json($event);
}
While the model looks like this:
public static function createFromArray($data) {
// Create a new event with the given data.
$event = self::create([
'title' => $data['title'],
'description' => $data['description'],
'place' => $data['place'],
'time_happening' => $data['time_happening'],
'created_by' => auth()->user()->id,
'group_id' => $data['group_id']
]);
// Return the newly created event.
return $event
}
The JSON I'm writing in the API Client:
{
"title": "Laravel Tech Meetup.Laravel Tech Meetup.Laravel Tech
Meetup.Laravel Tech Meetup.",
"description": "Qui molestiae consectetur ipsam vel
dicta. Nesciunt architecto laborum accusantium
eos facere temporibus iure fugiat.
Et dicta incidunt ut deleniti eum alias earum.",
"place": "83224 Blabla",
"time_happening": "2019-09-25 03:29:46"
}
With this code I expect for the new instance to:
- Get created by the user I'm logged in as,
- And to get returned to me in the API Client I'm using(Insomnia)
Result:
I get redirected to Homepage and when I check if it's created in the database it's not.
What am I doing wrong?!