How to Generate Avatar Image for User in Laravel?
In this post, I will show you how to create dynamic user avatar in laravel application.
User avatars are a great way to add a personal touch to user profiles in web applications. In this tutorial, we'll demonstrate how to generate dynamic image avatars in Laravel using the first two letters of a user's name. To achieve this, we'll utilize the laravolt/avatar composer package to create custom avatar images in a Laravel application.
Let's follow the steps to do the examples:
Step 1: Install Laravel
This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:
composer create-project laravel/laravel example-app
Step 2: Install laravolt/avatar
Now, in this step, we will install laravolt/avatar composer package. So, run the following command:
composer require laravolt/avatar
Step 3: Setup Avatar
Here, we will display dynamic user avatar using the following method:
Avatar::create('Hardik Savani')->toSvg();
You can see the full user list like the following way:
resources/views/users/index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel Example</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="card mt-5">
<div class="card-header"><h4>User List</h4></div>
<div class="card-body" id="table-data">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{!! Avatar::create($user->name)->setDimension(35, 35)->setFontSize(15)->toSvg() !!} {{ $user->name }}</td>
<td>{{ $user->email }}</td>
</tr>
@endforeach
</tbody>
</table>
{{ $users->links("pagination::bootstrap-5") }}
</div>
</div>
</div>
</body>
</html>
Run Laravel App:
All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:
php artisan serve
Now, Go to your web browser, type the given URL and view the app output:
http://localhost:8000/users
You can see the output:
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- How to Convert an Array into Object in Laravel?
- PHP Laravel Generate Apple Wallet Pass Example
- Laravel Breeze Login with Google Auth Example
- Laravel Datatables Relationship with Filter Column Example
- Laravel 11 Display Image from Storage Folder Example
- Laravel 11 Store JSON Format Data in Database Tutorial
- How to Integrate AdminLTE 3 in Laravel 11?
- Laravel 11 Event Broadcasting Tutorial
- Laravel 11 Confirm Box Before Delete Record from Database
- Laravel 11 Localization | Create Multi Language in Laravel 11
- Laravel 11 Client Side Form Validation using JQuery
- Laravel 11 Pagination Add Query String Example
- Laravel 11 Add or Remove Multiple Input Fields with jQuery Example
- Laravel 11 Create Blade File using Command Example