@@ -19,6 +19,13 @@ public function index()
1919 ->when (request ("search " ), function ($ query ) {
2020 return $ query ->whereRaw ('LOWER(title) LIKE ? ' , ['% ' . strtolower (request ('search ' )) . '% ' ]);
2121 })
22+ ->when (request ("tags " ), function ($ query ) {
23+ $ tags = explode (', ' , request ('tags ' ));
24+ return $ query ->whereHas ('tags ' , function ($ query ) use ($ tags ) {
25+ $ query ->whereIn ('name ' , array_map ('trim ' , $ tags ));
26+ });
27+ })
28+ ->with ('tags ' ) // Eager load tags
2229 ->withCount ('comments ' )
2330 ->latest ()
2431 ->paginate (10 ); // Eager load user and categories
@@ -49,21 +56,39 @@ public function store(Request $request)
4956 'content ' => 'required ' ,
5057 'categories ' => 'array ' ,
5158 'feature_image ' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048 ' ,
59+ 'tags ' => 'array ' ,
60+ 'tags.* ' => 'string|max:255 ' ,
5261 ]);
62+ \DB ::beginTransaction ();
63+ try {
64+ $ tags = $ request ->input ('tags ' , []);
65+ $ tagIds = [];
66+ foreach ($ tags as $ tagName ) {
67+ $ tag = \App \Models \Tag::firstOrCreate (['name ' => $ tagName ]);
68+ $ tagIds [] = $ tag ->id ;
69+ }
70+ $ post = auth ()->user ()->posts ()->create ($ validated );
5371
54- $ post = auth ()->user ()->posts ()->create ($ validated );
55- if ($ request ->hasFile ('feature_image ' )) {
56- $ post ->addMediaFromRequest ('feature_image ' )->toMediaCollection ('feature_images ' );
57- }
58- $ post ->categories ()->sync ($ request ->categories );
59- $ post ->load ('user ' , 'categories ' , 'media ' )->loadCount ('comments ' );
72+ if ($ request ->hasFile ('feature_image ' )) {
73+ $ post ->addMediaFromRequest ('feature_image ' )->toMediaCollection ('feature_images ' );
74+ }
75+
76+ $ post ->categories ()->sync ($ request ->categories );
77+ $ post ->tags ()->sync ($ tagIds );
78+ $ post ->load ('user ' , 'categories ' , 'media ' , 'tags ' )->loadCount ('comments ' );
79+ // Sync tags
80+ \DB ::commit ();
81+ return response ()->json (new \App \Http \Resources \PostResource ($ post ), 201 );
82+ } catch (\Exception $ e ) {
83+ \DB ::rollBack ();
6084
61- return response ()->json (new \App \Http \Resources \PostResource ($ post ), 201 );
85+ return response ()->json (['error ' => 'Post creation failed. ' ], 500 );
86+ }
6287 }
6388
6489 public function show (Post $ post )
6590 {
66- $ post ->load (['user ' , 'categories ' , 'comments ' ])->loadCount ('comments ' );
91+ $ post ->load (['user ' , 'categories ' , 'comments ' , ' tags ' ])->loadCount ('comments ' );
6792 return response ()->json (new \App \Http \Resources \PostResource ($ post ));
6893 }
6994
@@ -77,15 +102,25 @@ public function update(Request $request, Post $post)
77102 'content ' => 'string ' ,
78103 'categories ' => 'array ' ,
79104 'feature_image ' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048 ' ,
105+ 'tags ' => 'array ' ,
106+ 'tags.* ' => 'string|max:255 ' ,
80107 ]);
81108
82109 if ($ request ->hasFile ('feature_image ' )) {
83110 $ post ->addMediaFromRequest ('feature_image ' )->toMediaCollection ('feature_images ' );
84111 }
85-
112+
86113 $ post ->update ($ validated );
114+ if ($ request ->has ('tags ' )) {
115+ $ tagIds = [];
116+ foreach ($ request ->input ('tags ' ) as $ tagName ) {
117+ $ tag = \App \Models \Tag::firstOrCreate (['name ' => $ tagName ]);
118+ $ tagIds [] = $ tag ->id ;
119+ }
120+ $ post ->tags ()->sync ($ tagIds );
121+ }
87122 $ post ->categories ()->sync ($ request ->categories );
88- $ post ->load ('user ' , 'categories ' , 'media ' )->loadCount ('comments ' );
123+ $ post ->load ('user ' , 'categories ' , 'media ' , ' tags ' )->loadCount ('comments ' );
89124 return response ()->json (new \App \Http \Resources \PostResource ($ post ));
90125 }
91126
0 commit comments