[ReflectionException]
Method create does not exist
Hi Ashok
You should really post more of your code if you would like a response. For example, you should post your routes file as a minimum and more of the error message you received. From the limited information provided, it looks like you are missing the Create route in one of your controllers.
If you are using a Laravel resource this automatically creates all the standard CRUD routes. You can exclude routes you do not want using with an except array:
Route::resource('yourRoute', 'YourController', array('except' => array('create', 'edit') ));
If you set up your own routes manually, you need to ensure the Create method exists on your controller (if you are using controllers).
I hope that helps a bit....
P.s. This is also not likely to be bug in the project code (just your code), so this type of question is better posted on a site like StackOverflow.com so you do not overwhelm the authors of projects like this who give their time for free for our benefit.
Thank you so much I got it 馃憤
@spirant what did u mean by this
you need to ensure the Create method exists on your controller (if you are using controllers). ???
Hi @abdolrhman
If you are using Laravel Resources then a number of routes will be created automatically (such as create, edit, index, etc.):
Route::resource('yourRoute', 'YourController', array('except' => array('create', 'edit') ));
All routes must have a method on the controller matching them, or the route should be explicitly excluded (or you would see the type of error message @ashokTagtaste had as the API document generator will try to run the route and will not find the required method on the controller). He most likely did not have the Create method on the Controller, but without him posting his code I can only guess.
Regarding my comment (if you are using controllers).: you are not required by Laravel to use Controllers at all. You can do all your coding inside the routes/web.php file. This is not recommended as you get long files and it is poor to do so in practice.
Route::get('YourModelName/create/', function(){
// Your coding could go in here, but this is not best practice...
});
If you are not using Laravel Resources then every route into your app must be individually defined. For example:
// In routes/web.php
// Every route into your model must be defined
Route::get('YourModelName/create/', 'YourModelNameController@create');
Route::get('YourModelName/index/', 'YourModelNameController@index');
Route::post('YourModelName/restore/{id}', 'YourModelNameController@restore');
// Etc....
Either way you need to ensure your controller has a method for all possible routes in (or as mentioned in my previous answer you should exclude the routes you do not require). If you are building an API you may not require a route at all for Create, as this is the route needed to return any response needed to create the form your user will fill in to create a new resource. Under the standard Laravel Resource the Create method does not acutally Create a new resource, that is done by the Store method. An API may not need to consider this route as it is something relevant to the client side of an App rather than to do with an API.
// In your controller you must ensure that you have the following methods
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
/**
* @resource YourModelName
*
* The available Operating Manuals
*/
class YourModelNameController extends Controller {
/**
* create- YourModelName
*
* Show form to Create model
*
* @return Response
*/
public function create(){
// This method was most likely missing from the Controller for @ashokTagtaste
}
/**
* index - YourModelName
*
* Return a listing of the resource.
*
* @return Response
*/
public function index()
{
// Your Response
}
/**
* restore - YourModelName
*
* Restore / recover soft deleted model
*
* @param [integer] $id
* @return Response
*/
public function restore($id){
// Your Response
}
}
@spirant I encountered the same issue, but in my code I use the store method instead of create is there a way around this?
Edit: Never mind, I realized that the create method isn't used for storing. But still, what if a resource doesn't have all the methods? It shouldn't throw an error, but just skip those.
Hi @paxy97 . The Api Document Generator can skip missing methods but, only if you tell it to do so (as set out in the example except array below where the create and edit routes are excluded):
Route::resource('yourRoute', 'YourController', array('except' => array('create', 'edit') ));
Although you feel that this may be a bug, by using the resource route you are in fact saying that all the routes do exist (unless excluded in except array) and they may be served to the API consumer with a page missing error, which is not best practice. It is far better to remove the routes which should not be accessible to the consumers than leave them there with 404 warnings when they try to use those routes. I find this warning message a useful feature for ensuring that you have created all methods the API will expect to exist. Add all the missing routes highlighted by the API Doc Generator to each except array on each resource and your API will be far more robust.
Hmm, makes sense. I'll get around to adding the exceptions then.
Thank you for the help
Not a problem ;-)
Most helpful comment
Hi @paxy97 . The Api Document Generator can skip missing methods but, only if you tell it to do so (as set out in the example except array below where the create and edit routes are excluded):
Route::resource('yourRoute', 'YourController', array('except' => array('create', 'edit') ));Although you feel that this may be a bug, by using the resource route you are in fact saying that all the routes do exist (unless excluded in except array) and they may be served to the API consumer with a page missing error, which is not best practice. It is far better to remove the routes which should not be accessible to the consumers than leave them there with 404 warnings when they try to use those routes. I find this warning message a useful feature for ensuring that you have created all methods the API will expect to exist. Add all the missing routes highlighted by the API Doc Generator to each
exceptarray on each resource and your API will be far more robust.