Question :
I’m learning how to build an API using Laravel 5.4. *.
No Api.php
:
$this->get('products', 'APIProductController@index', ['except' => [
'create', 'edit'
]]);
No Product.php
:
class Product extends Model
{
protected $fillable = ['name', 'description'];
}
No ProductController.php
:
private $product;
public function __construct(Product $product)
{
$this->product = $product;
}
public function index()
{
$products = $this->product->all();
return response()->json(['data' => $products]);
}
public function store(Request $request)
{
return response()->json([
'result' => $this->product->create($request->all())
]);
}
I reviewed my code and could not find any errors.
When I use GET it returns the products registered, however when I use POST I get this error:
What can it be?
Answer :
Your code is correct and the error is also:)
$this->get('products'
This route specifies that only requests through the get method will be processed otherwise it will return the code 405 method not allowed. Conceptually research is done through the get method, to create / add new features the post is used.
Recommended reading:
What are the advantages of using the correct HTTP methods