Skip to content

Commit f8d079d

Browse files
Add user_name and user_imageUrl to post
1 parent a0f3a70 commit f8d079d

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

app/Http/Controllers/Api/PostController.php

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use App\Http\Controllers\Controller;
66
use Illuminate\Http\Request;
77
use App\Models\Post;
8+
use App\Models\User;
89
use Illuminate\Support\Facades\Auth;
910
use Illuminate\Support\Facades\Validator;
1011
use App\Http\Requests\PostRequest;
@@ -15,25 +16,45 @@ class PostController extends Controller
1516
{
1617
use ApiTrait, Media;
1718

18-
public function getPosts()
19-
{
20-
$posts = Post::orderBy('created_at', 'desc')->get();
21-
return $this->data(compact('posts'));
22-
}
19+
public function getPosts()
20+
{
21+
// Retrieve all posts with the user relationship and order them by 'created_at' in descending order
22+
$posts = Post::with('user')->orderBy('created_at', 'desc')->get();
23+
24+
// Transform the posts data to include user_name and user_imageUrl
25+
$postData = $posts->map(function ($post) {
26+
$data = $post->toArray();
27+
$data['user_name'] = $post->user->name; // Change 'name' to the actual column name in your users table
28+
$data['user_imageUrl'] = $post->user->imageUrl; // Change 'imageUrl' to the actual column name in your users table
29+
unset($data['user']); // Remove the user relationship to avoid redundancy
30+
return $data;
31+
});
32+
33+
return $this->data(compact('postData'));
34+
}
35+
2336

2437
public function showPost($id)
2538
{
26-
$post = Post::find($id);
27-
39+
$post = Post::with('user')->find($id);
40+
2841
if (!$post) {
2942
return $this->errorMessage([], 'Post not found', 404);
3043
}
31-
44+
45+
// Convert the Post model and user model to an array
3246
$postData = $post->toArray();
33-
47+
48+
// Replace the user_id with user's name and imageUrl
49+
$postData['user_name'] = $post->user->name; // Change 'name' to the actual column name in your users table
50+
$postData['user_imageUrl'] = $post->user->imageUrl; // Change 'imageUrl' to the actual column name in your users table
51+
52+
// Unset the user relationship to avoid redundancy
53+
unset($postData['user']);
54+
3455
return $this->data($postData, 'Post retrieved successfully', 200);
3556
}
36-
57+
3758

3859
public function createPost(PostRequest $request)
3960
{
@@ -51,12 +72,12 @@ public function createPost(PostRequest $request)
5172
$post->content = $request->input('content');
5273
}
5374

54-
// Handle image upload, if provided
55-
if ($request->hasFile('file_path')) {
75+
// Handle image upload, if provided
76+
if ($request->hasFile('file_path')) {
5677
$image = $request->file('file_path');
5778
$imagePath = $this->upload($image, 'posts');
5879
$post->image_path = "images/posts/$imagePath";
59-
}
80+
}
6081

6182

6283
$post->save();
@@ -85,11 +106,11 @@ public function editPost(PostRequest $request, $id)
85106
}
86107

87108
// Handle image upload, if provided
88-
if ($request->hasFile('file_path')) {
109+
if ($request->hasFile('file_path')) {
89110
$image = $request->file('file_path');
90111
$imagePath = $this->upload($image, 'posts');
91112
$post->image_path = "images/posts/$imagePath";
92-
}
113+
}
93114

94115
$post->save();
95116

database/migrations/2014_10_12_000000_create_users_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function up()
1818
$table->string('name');
1919
$table->string('email')->unique();
2020
$table->enum('role', ['admin', 'sub-admin', 'reviewer', 'user'])->default('user');
21+
$table->string('imageUrl')->nullable();
2122
$table->integer('code')->nullable();
2223
$table->timestamp('code_expired_at')->nullable();
2324
$table->timestamp('email_verified_at')->nullable();

0 commit comments

Comments
 (0)