#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;
-
routes > api.php
-
Test api Route
Route::get('/test', function(Request $request){
return 'Authenticated';
});
- step(1)
-
step(2)
-
copy access token
-
step(3)
-
paste
key => Authorization
andvalue => Bearer (access token)
-
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,
]
];
// 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
toture
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
toture
in UpdateAuthorRequest
public function rules()
{
return [
'name' => 'required|unique:authors|max:255'
];
}
- AuthorsController > destroy
$author->delete();
return response(null, 204);
-
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',
];