API Rest with Laravel 5.6 Passport Authentication — Generate avatar (Part 3)
By: Alfredo Barron
Tags: Laravel, API, Avatar, Rest, Authentication

We will learn to generate avatar for user accounts, in the authentication system with Laravel API Authentication Passport.

This tutorial is continuation of the API Rest with Laravel 5.6 Passport Authentication — Confirm account + notifications (Part 2) tutorial.

Step 1. Install Avatar Package

In first step, we require install to Laravolt Avatar package using bellow command, So open your terminal or command prompt and run bellow command:

composer require laravolt/avatar

We can publish the config service provider (Optional)

php artisan vendor:publish --provider="Laravolt\Avatar\ServiceProvider"

This will create config file located in config/laravolt/avatar.php

Step 2. Add column in users table

We add a column avatar in database/migrations/xxxx_create_users_table.php migration file.

<?php use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;

{

/**

* Run the migrations.

*

*

*/

public function up()

{

Schema::create('users', function (Blueprint $table) {

$table->increments('id');

$table->string('name');

$table->string('email')->unique();

$table->string('password');

$table->string('avatar')->default('avatar.png');

$table->boolean('active')->default(false);

$table->string('activation_token');

$table->rememberToken();

$table->timestamps();

$table->softDeletes();

});

}

... class CreateUsersTable extends Migration/*** Run the migrations. @return void*/public function up()Schema::create('users', function (Blueprint $table) {$table->increments('id');$table->string('name');$table->string('email')->unique();$table->string('password');$table->boolean('active')->default(false);$table->string('activation_token');$table->rememberToken();$table->timestamps();$table->softDeletes();});...

Next, we add fillable attribute in your App\User model.

<?php ... class User extends Authenticatable

{

... protected $fillable = [

... 'password', 'active', 'activation_token', 'avatar'

]; }

After, open your terminal or command prompt and run bellow command:

php artisan migrate:refresh

Step 3. Create avatar for user account

We have to update app/Http/Controllers/AuthController.php controller and update signup api method. So let’s update AuthController and put bellow code:

<?php use Avatar;

use Storage; class AuthController extends Controller

{

... public function signup(Request $request)

{

$request->validate([

'name' => 'required|string',

'email' => 'required|string|email|unique:users',

'password' => 'required|string|confirmed'

]); $user = new User([

'name' => $request->name,

'email' => $request->email,

'password' => bcrypt($request->password),

'activation_token' => str_random(60)

]); $user->save(); $avatar = Avatar::create($user->name)->getImageObject()->encode('png');

Storage::put('avatars/'.$user->id.'/avatar.png', (string) $avatar); $user->notify(new SignupActivate($user)); return response()->json([

'message' => 'Successfully created user!'

], 201);

}

}

The avatar will create in to storage/avatars folder.

Step 4. Get avatar of authenticated user

Finally, we add appends attribute in your App\User model to get avatar url.

<?php ...

use Storage; class User extends Authenticatable

{

... protected $appends = ['avatar_url']; public function getAvatarUrlAttribute()

{

return Storage::url('avatars/'.$this->id.'/'.$this->avatar);

} }

Now we are ready to run our example so run bellow command to quick run: