0
votes

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: Illuminate\Database\Query\Builder::$map". and it doesn't give json data. look to the link below

api.php

Route::POST('register', 'App\Http\Controllers\Api\UserController@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 App\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Http\Resources\Json\Resource;

class UserResource extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    public function toArray($request)
    {
        return parent::toArray($request);
    }
}
1
Try using form data .Ashickur Rahman
which form data you talking about?Hisham Zeyad
Sorry, try to use Params instead of body to submit data to the end pointAshickur Rahman
please,write the code how will becomeHisham Zeyad
If you use the postman extension throw it away and use the postman main application.Tohid Dadashnezhad

1 Answers

0
votes

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 Illuminate\Http\Resources\Json\JsonResource;

class UserResource extends JsonResource