Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

littleboby's avatar

Cannot create new RESTful API Instance

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?!

0 likes
8 replies
Punksolid's avatar

Hi @littleboby By restful standard it is common to use the method store to save resources and created is commonly used to show the html to do so.

So if you are using Resource::controller('EventController') it will be trying to receive a GET for the create method, and a POST for the store method.

Try changing to the store method in your controller and make sure you are making a POST request to your api/events route.

Or instead you could also add a Route::post('api/events', 'EventController@create') before your Resource declaration.

How is your api.php route defined?

littleboby's avatar

Thanks for the answer @punksolid, I just switched from create to store. The Resource::controller('EventController') is in other methods of this controller, if that makes any difference to my situation?

My route for this method looks like this:

Route::post('event/', 'EventController@store');

littleboby's avatar

Also: If I open my Telescope panel this request is shown as a GET request and I don't get it

Punksolid's avatar

Well if your route is defined Route::post('event/', 'EventController@store'); or Route::resource('event', 'EventController'); it is the same, both will search for EventController@store when doing a post request to the resource route.

It looks that you are doing the request wrong. Where or how are you making your request?

Snapey's avatar

How are you authenticating yourself in insomnia?

Perhaps some simple debugging checks are required?

littleboby's avatar

@punksolid & @snapey - So in my api.php file I have this route -

Route::post('event/', 'EventController@store');

which calls the aforementioned method.

In my Insomnia Client I'm doing the request with the following json body and the following credentials - Body: https://imgur.com/ILgyz2e , Authentication: https://imgur.com/Rf93al3 , Telescope Feedback: https://imgur.com/8uPs1QU - , which I now see uses the SinglePageController and gets me redirected.

This controller looks like this:

class SinglePageController extends Controller {
    public function index() {
        return view('discover');
    }
}

which is the main class for the following route(s):

Route::get('/{any}', 'SinglePageController@index')->where('any', '.*');
Punksolid's avatar

Hi @littleboby I'm not familiar with insomnia but it looks that you're having a 200 OK response. So I think that what it could be failing is that you may not be sending the right headers.

Try sending this headers Content-Type:application/json;charset=UTF-8, Accept:application/json

Why are you using the where in the route? Basically you are specifying that whatever the route is with a GET is gonna be redirected, try making it more explicit

littleboby's avatar

Thanks for the answer, now in my web.php file I replaced the earlier statement with the following:

Route::get('/{any?}', function (){
    return view('discover');
})->where('any', '^(?!api\/)[\/\w\.-]*');

and the store method like so:

public function store(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)->withHeaders([
            'Content-Type' => 'application/json;charset=UTF-8, Accept:application/json',
        ]);
    }

Telescope shows this:

content-type: "application/json",

and it still doesn't seem to work. Trying this syntax in Insomnia or Postman gives an error saying:

The GET method is not supported for this route. Supported methods: POST.

Could I make the route statement better? What should I make to fix this?

Please or to participate in this conversation.