naylinhtunit / bookstore-laravel-api

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

#Laravel API

  • My note

  • Laravel 8*

  • composer require laravel/passport

  • php artisan migrate

  • php artisan passport:install

  • app > Models > User.php

  • use Laravel\Passport\HasApiTokens;

  • use HasFactory, Notifiable, hasApiTokens;

  • app > Providers > AppServiceProvider.php

  • use Laravel\Passport\Passport;

  • We need to call the passport route inside the boot method

  • Passport::routes();

  • config > auth.php

  • change 'driver' => 'token', to 'driver' => 'passport',

  • php artisan tinker

DB::table('users')->insert(['name'=>'aung aung', 'email'=>'info@bookstore.com', 'password'=>Hash::make('info1234')]);
  • exit;

Access Token

  • routes > api.php

  • Test api Route

Route::get('/test', function(Request $request){
    return 'Authenticated';
});
  • step(1)

1

  • step(2)

  • copy access token

2

  • step(3)

  • paste key => Authorization and value => Bearer (access token)

3

Building Resources

  • Model, Factory, Table, Seeder and Controller

  • php artisan make:model -a -r Author

  • Database > factories > AuthorFactory.php

  • 'name' => $this->faker->name

  • Database > seeders > AuthorSeeder.php

  • \App\Models\Author::factory(5)->create();

  • Database > seeders > DatabaseSeeder.php

  • $this->call(AuthorSeeder::class);

  • php artisan migrate --seed

  • Resource create php artisan make:resource AuthorsResource

  • AuthorsController

  • use App\Http\Resources\AuthorsResource;

  • return new AuthorsResource($author);

  • app > Http > Resources > AuthorResource

return [
    // 'id' => $this->id,
    //change string
    'id' => (string)$this->id,
        'type' => 'Authors',
        'attributes' => [
            'name' => $this->name,
            'created_at' => $this->created_at,
            'updated_at' => $this->updated_at,
        ]
];

CRUD REST API WITH LARAVEL

// Route::get('/authors', [AuthorsController::class, 'index']);
// Route::post('/authors', [AuthorsController::class, 'store']);
// Route::get('/authors/{author}', [AuthorsController::class, 'show']);

Route::apiResource('/authors', AuthorsController::class);
  • AuthorsController > store
$faker = \Faker\Factory::create(1);

$author = Author::create([
    'name' => $faker->name
]);

return new AuthorsResource($author);
  • Change false to ture in StoreAuthorRequest
 public function rules()
{
    return [
        'name' => 'required|unique:authors|max:255'
    ];
}
  • AuthorsController > update
$author->update([
    //'name' => 'Kalin'
    'name' => $request->input('name')
]);

return new AuthorsResource($author);
  • Change false to ture in UpdateAuthorRequest
 public function rules()
{
    return [
        'name' => 'required|unique:authors|max:255'
    ];
}
  • AuthorsController > destroy
$author->delete();
return response(null, 204);

Creating to a bookstore

  • php artisan make:model Book -a

  • php artisan make:migration create_book_author_table

  • php artisan make:resource BooksResource

  • php artisan tinker

DB::table('book_author')->insert(['author_id'=>2, 'book_id'=>1]);
  • php artisan make:model BookAuthor -a

  • Models > BookAuthor

public function author()
{
    return $this->hasManyThrough(
        'App\Models\Author',
        'App\Models\BookAuthor',
        'book_id',
        'id',
        'id',
        'author_id',
    );
}
  • you can hidden in author model
protected $hidden = [
    'laravel_through_key',
    'created_at',
    'updated_at',
];

About


Languages

Language:PHP 84.6%Language:Blade 14.8%Language:Shell 0.6%