You sent the api request to /api/v1/trials? The api group (api.php) automatically prepends /api to the api routes.
API route returning 404, but Web route works
So, I have two routes setup. One route is in web.php.
Route::post('/trials', 'TrialsController@store');
And the API version of it is in api.php:
Route::post('/v1/trials', 'TrialsController@store');
I created a simple form for myself to test the web route from the browser, and can successfully store a trial by POSTing to the web route above. However, from Postman when I try to store a trial by POSTing to the API route above, I get a 404 in the response.
Here is my TrialsController's store method.
{
// Here we are after successful validation of the request
// First, we create the firm associated with the trial.
$firm = Firm::create([
'name' => $request->name,
'aum' => $request->aum,
]);
// Then, we create a new contact in the database for the firm.
$contact = $firm->contacts()->save(new Contact([
'first_name' => $request->first_name,
'last_name' => $request->last_name,
'email' => $request->email,
'phone' => $request->phone
]));
// Finally, we create the new initial trial in the database for the contact.
$trial = $contact->trials()->save(new Trial([
'starts_on' => $request->starts_on,
'ends_on' => $request->ends_on,
'repeated' => 0
]));
// All is saved to the database, now return the new intial trial.
return (new TrialResource($trial));
}
The StoreTrial form request rules' method is as follows.
{
return [
'name' => 'required | min:3',
'aum' => 'numeric',
'first_name' => 'required',
'last_name' => 'required',
'email' => 'required',
'phone' => 'required',
'starts_on' => 'required',
'ends_on' => 'required'
];
}
Is this related to special authentication that I need to setup for API calls? I am a bit lost as to why web route works but api route does not. Can two routes not hit the same controller/method? Appreciate any help. Thanks!
How else would Laravel know that I am using the "api" route instead of the web route?
How are we supposed to know you're hitting the correct route? You don't show any code showing the actual url you are hitting. You say you are, but you're also getting a 404 so the first logical step is to find out if you were actually hitting the correct endpoint.
Please or to participate in this conversation.