when i access to endpoint this http://localhost/newsapp_api/public/api/register,this message is showed “The GET method is not supported for this route. Supported methods: POST”. look to the link below
but when i tried register new user and entered data(name,email,password) for user by postman program this message is showed “message”: “Undefined property: IlluminateDatabaseQueryBuilder::$map”. and it doesn’t give json data. look to the link below
api.php
Route::POST('register', 'AppHttpControllersApiUserController@store');
UserController.php
public function store(Request $request) { $request->validate([ 'name' => 'required', 'email' => 'required', 'password' => 'required' ]); $user = new User(); // $user->name = $request->get( 'name' ); // $user->email = $request->get( 'email' ); // $user->password = Hash::make( $request->get( 'password' ) ); $user->name = $request->name; $user->email = $request->email; $user->password = Hash::make( $request->password ); $user->save(); return new UserResource( $user ); }
UserResource.php
<?php namespace AppHttpResources; use IlluminateHttpResourcesJsonResourceCollection; use IlluminateHttpResourcesJsonResource; class UserResource extends ResourceCollection { /** * Transform the resource collection into an array. * * @param IlluminateHttpRequest $request * @return array */ public function toArray($request) { return parent::toArray($request); } }
Advertisement
Answer
Your first problem when you enter your endpoint in the browser, the browser will send GET method to your endpoint. Since your endpoint only accepts POST method, it throws an error.
The second problem is you extends UserResource with ResourceCollection so UserResource expects a collection. However you pass a User object which is not a collection to UserResource.
I think you meant to create a UserResource as just a resource which extends JsonResource
https://laravel.com/docs/8.x/eloquent-resources#introduction
use IlluminateHttpResourcesJsonJsonResource; class UserResource extends JsonResource