Kritim Yantra
Jul 01, 2025
π Introduction: Profile Images Matter!
Whether you're building a social network, an admin panel, or a blogging platform β allowing users to upload profile images makes your app more personal, interactive, and professional.
Luckily, Laravel 12 makes this task simple and elegant. π§βπ»
In this tutorial, weβll walk you through step-by-step how to upload a userβs profile image and store it properly in your Laravel app β without confusion or complexity. Even if you're new to Laravel, youβll be able to follow along easily. π
Make sure you have the following set up:
If not, you can install Laravel quickly using:
composer create-project laravel/laravel laravel-profile-upload
First, letβs modify the users
table to store the profile image path.
php artisan make:migration add_profile_image_to_users_table --table=users
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('profile_image')->nullable()->after('email');
});
}
php artisan migrate
Add profile_image
to the $fillable
array in app/Models/User.php
:
protected $fillable = [
'name',
'email',
'password',
'profile_image', // β
Add this
];
Now letβs build a Blade form where users can upload their profile picture.
resources/views/profile.blade.php
):<form action="{{ route('profile.upload') }}" method="POST" enctype="multipart/form-data">
@csrf
<div>
<label for="profile_image">Choose Profile Image</label>
<input type="file" name="profile_image" id="profile_image">
</div>
<button type="submit">Upload</button>
</form>
π‘ Tip: Always set enctype="multipart/form-data"
for file uploads.
Open routes/web.php
and define the upload route:
use App\Http\Controllers\ProfileController;
Route::post('/profile/upload', [ProfileController::class, 'upload'])->name('profile.upload');
Generate the controller (if you donβt have one already):
php artisan make:controller ProfileController
Now add the upload
method to handle the file upload:
app/Http/Controllers/ProfileController.php
public function upload(Request $request)
{
$request->validate([
'profile_image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
]);
$user = auth()->user();
if ($request->hasFile('profile_image')) {
$image = $request->file('profile_image');
$name = time().'_'.$image->getClientOriginalName();
$path = $image->storeAs('profile_images', $name, 'public');
// Optional: delete old image
if ($user->profile_image) {
Storage::disk('public')->delete($user->profile_image);
}
$user->profile_image = $path;
$user->save();
}
return back()->with('success', 'Profile image uploaded successfully!');
}
π This saves the image to storage/app/public/profile_images
.
π Make sure you have a symbolic link:
php artisan storage:link
@if (auth()->user()->profile_image)
<img src="{{ asset('storage/' . auth()->user()->profile_image) }}" alt="Profile Image" width="150">
@else
<img src="{{ asset('default-avatar.png') }}" alt="Default Image" width="150">
@endif
Think of your Laravel app as a digital passport system.
Uploading a profile image is like pasting a photo on your passport. It helps the system β and others β recognize and personalize your experience.
Just like in real life, you need a clear and secure way to store and show that photo β and Laravel provides all the tools you need!
profile_images/user_123/
)multipart/form-data
and Laravelβs storeAs()
method to save files securelyphp artisan storage:link
to access images publiclyasset('storage/β¦')
And there you go β youβve just learned how to upload and display user profile images in Laravel 12 step-by-step! π₯³
This is an essential feature for any modern web app and opens the door to even more advanced media handling.
Got stuck somewhere? Let us know in the comments or reach out β happy coding! π¨βπ»π©βπ»
No comments yet. Be the first to comment!
Please log in to post a comment:
Sign in with Google