Laravel Short Note



Create API Controller 

    php artisan make:controller BookController --resource --model=Book --api


Route Example

    Route::apiResource('/books', 'api\BookApiController');


Simple Response

public function index()
{
     $books = Book::paginate();
     return Response()->json($books, 200,[], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}

Create And Use Resource

       php artisan make:resource UserCollection

    --------------------------------------

    public function toArray($request)

    {

    return [

        ...

        'posts' => PostResource::collection($this->posts),

        ...

    ]

    ------------------------

    return UserResource::collection(User::all());


Problem installing passport?    

    First try 

            composer require laravel/passport:7.5.1

   If it doesn't work then, 

         composer require laravel/passport:7.5.1 --with-all-dependencies

        

How to install api login feature with passport

Laravel 5.8 Passport Authentication | Create REST API with Passport authentication - W3Schools | Tutorialspoint | W3Adda


Auth Api redirecting to login?
while requesting add accept: applicaiton/json in header (not in laravel)



Thank You.

Comments