Skip to content

Commit 6d83950

Browse files
Add user profile avatar storing functionality
1 parent 5878c48 commit 6d83950

File tree

5 files changed

+50
-22
lines changed

5 files changed

+50
-22
lines changed

app/Http/Controllers/UserController.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Http\Requests\UserStoreRequest;
66
use App\Models\User;
77
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Storage;
89
use Inertia\Inertia;
910

1011
class UserController extends Controller
@@ -34,9 +35,16 @@ public function create()
3435
*/
3536
public function store(UserStoreRequest $request)
3637
{
37-
User::create($request->validated());
38+
$validatedData = $request->validated();
3839

39-
return to_route('users.index')->with('success', 'User has been created!');
40+
if ($request->hasFile('avatar')) {
41+
$avatarPath = $request->file('avatar')->store('public/avatars');
42+
$validatedData['avatar'] = Storage::url($avatarPath);
43+
}
44+
45+
User::create($validatedData);
46+
47+
return redirect()->route('users.index')->with('success', 'User has been created!');
4048
}
4149

4250
/**
@@ -86,4 +94,4 @@ public function deleteMultiple(Request $request)
8694

8795
return redirect()->route('users.index')->with('success', 'Operation successfully!');
8896
}
89-
}
97+
}

app/Http/Requests/UserStoreRequest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public function rules(): array
2626
'email' => 'required|email|min:2|max:255|unique:users',
2727
'password' => 'required|min:8|max:255',
2828
'role_id' => 'required',
29+
'avatar' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
2930
];
3031
}
3132
}

database/migrations/2014_10_12_000000_create_users_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ public function up(): void
1616
$table->id();
1717
$table->string('name');
1818
$table->string('email')->unique();
19+
$table->string('avatar')->default('placeholder');
1920
$table->timestamp('email_verified_at')->nullable();
2021
$table->string('password');
2122
$table->rememberToken();
2223
$table->foreignId('current_team_id')->nullable();
23-
$table->string('profile_photo_path', 2048)->nullable();
2424
$table->bigInteger('role_id')->unsigned()->index()->nullable();
2525
// $table->foreign('role_id')->references('id')->on('roles')->cascadeOnDelete();
2626
$table->timestamps();

resources/js/Pages/Users/Create.vue

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,62 @@
22
<AppLayout title="Create user">
33

44
<template #header>
5-
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
5+
<h2 class="text-xl font-semibold leading-tight text-gray-800">
66
Create user
77
</h2>
88
</template>
99

1010
<div class="py-12">
11-
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
12-
<div class="m-auto w-full max-w-xs">
11+
<div class="mx-auto max-w-7xl sm:px-6 lg:px-8">
12+
<div class="w-full max-w-xs m-auto">
1313

14-
<form @submit.prevent="submit" class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4 m-auto">
14+
<form @submit.prevent="submit" class="px-8 pt-6 pb-8 m-auto mb-4 bg-white rounded shadow-md"
15+
enctype="multipart/form-data">
1516

1617
<div class="mb-4">
17-
<label class="block text-gray-700 text-sm font-bold mb-2" for="name">
18+
<label class="block mb-2 text-sm font-bold text-gray-700" for="name">
1819
Name <span class="text-red-500">*</span>
1920
</label>
2021
<input v-model="form.name"
21-
class="mb-2 shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
22+
class="w-full px-3 py-2 mb-2 leading-tight text-gray-700 border rounded shadow appearance-none focus:outline-none focus:shadow-outline"
2223
id="name" type="text">
2324
<span class="text-red-500">{{ errors.name }}</span>
2425
</div>
2526

2627
<div class="mb-4">
27-
<label class="block text-gray-700 text-sm font-bold mb-2" for="email">
28+
<label class="block mb-2 text-sm font-bold text-gray-700" for="email">
2829
E-mail <span class="text-red-500">*</span>
2930
</label>
3031
<input v-model="form.email"
31-
class="mb-2 shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
32+
class="w-full px-3 py-2 mb-2 leading-tight text-gray-700 border rounded shadow appearance-none focus:outline-none focus:shadow-outline"
3233
id="email" type="email">
3334
<span class="text-red-500">{{ errors.email }}</span>
3435
</div>
3536

3637
<div class="mb-4">
3738
<div class="mb-3 xl:w-96">
3839
<select disabled v-model="form.role_id"
39-
class="form-select appearance-none block text-base font-normal text-gray-700 bg-white bg-clip-padding bg-no-repeat border border-solid border-gray-300 rounded ease-in-out m-0 focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none"
40+
class="block m-0 text-base font-normal text-gray-700 ease-in-out bg-white bg-no-repeat border border-gray-300 border-solid rounded appearance-none form-select bg-clip-padding focus:text-gray-700 focus:bg-white focus:border-blue-600 focus:outline-none"
4041
aria-label="Default select example">
4142
<option value="2">Subscriber</option>
4243
</select>
4344
</div>
4445
</div>
4546

4647
<div class="mb-4">
47-
<label class="block text-gray-700 text-sm font-bold mb-2" for="password">
48+
<label class="block mb-2 text-sm font-bold text-gray-700" for="avatar">
49+
Avatar <span class="text-red-500">*</span>
50+
</label>
51+
<input type="file" id="avatar" v-on:change="form.avatar = $event.target.files[0]">
52+
<span class="text-red-500">{{ errors.avatar }}</span>
53+
</div>
54+
55+
<div class="mb-4">
56+
<label class="block mb-2 text-sm font-bold text-gray-700" for="password">
4857
Password <span class="text-red-500">*</span>
4958
</label>
5059
<input v-model="form.password"
51-
class="mb- 2shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
60+
class="w-full px-3 py-2 leading-tight text-gray-700 border rounded appearance-none mb- 2shadow focus:outline-none focus:shadow-outline"
5261
id="password" type="password">
5362
<span class="text-red-500">{{ errors.password }}</span>
5463
</div>
@@ -58,7 +67,7 @@
5867
</div>
5968
</form>
6069

61-
<p class="text-center text-gray-500 text-xs">
70+
<p class="text-xs text-center text-gray-500">
6271
&copy;2023 - <a class="text-blue-500" href="https://github.com/perisicnikola37"
6372
target="_blank">@perisicnikola37</a>
6473
</p>
@@ -88,6 +97,7 @@ export default {
8897
email: '',
8998
role_id: 2,
9099
password: '',
100+
avatar: null
91101
})
92102
}
93103
},

resources/js/Pages/Users/Show.vue

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
<template>
22
<AppLayout title="User profile">
33
<div class="flex flex-col items-center justify-center min-h-screen bg-gray-100">
4-
<div class="max-w-lg w-full bg-white shadow-lg rounded-lg overflow-hidden">
5-
<img class="w-full h-64 object-cover" :src="user.coverImage" alt="User cover image">
4+
<div class="w-full max-w-lg overflow-hidden bg-white rounded-lg shadow-lg">
5+
<img class="object-cover w-full h-64" :src="user.coverImage" alt="User cover image">
66
<div class="p-4">
7-
<img class="h-24 w-24 rounded-full mx-auto mb-4" :src="user.profileImage" alt="User profile image">
7+
8+
<img class="w-24 h-24 mx-auto mb-4 rounded-full" :src="userAvatar" alt="User profile image">
9+
810
<h1 class="text-2xl font-bold text-center">{{ getUser.name }}</h1>
911
<center>
10-
<a :href="'mailto:' + getUser.email" class="text-gray-600 text-center">{{ getUser.email }}</a>
12+
<a :href="'mailto:' + getUser.email" class="text-center text-gray-600">{{ getUser.email }}</a>
1113
</center>
1214
<hr class="my-4">
1315
<div class="my-2">
14-
<h2 class="text-gray-600 text-sm font-bold uppercase">About</h2>
16+
<h2 class="text-sm font-bold text-gray-600 uppercase">About</h2>
1517
<p class="mt-2 text-gray-600">{{ user.about }}</p>
1618
</div>
1719
</div>
@@ -32,7 +34,14 @@ export default {
3234
},
3335
data() {
3436
return {
35-
user: {}
37+
user: {},
38+
}
39+
},
40+
computed: {
41+
userAvatar() {
42+
return this.getUser.avatar !== 'placeholder'
43+
? this.getUser.avatar
44+
: 'https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png';
3645
}
3746
},
3847
mounted() {

0 commit comments

Comments
 (0)