Skip to content

Commit 4a359a7

Browse files
authored
Merge pull request #15 from ninja1ndia/tags
Tags Module
2 parents b3df3f2 + aebbb64 commit 4a359a7

File tree

18 files changed

+315
-14
lines changed

18 files changed

+315
-14
lines changed

app/Http/Controllers/Admin/PostCategoryController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ public function update(Request $request, PostCategoryRepository $postCategoryRep
5858
'meta_title' => 'nullable|string',
5959
'meta_description' => 'nullable|string'
6060
]);
61-
61+
6262
$postCategory = PostCategory::findOrFail($id);
6363
$postCategory->fill($request->all());
6464
$postCategory->save();
6565
$postCategoryRepository->uploadImage($postCategory, $request, 'image');
66-
66+
6767
return redirect()->route('admin.postCategories.edit', $id)->with(['flash_type' => 'success', 'flash_message' => 'Post category updated successfully', 'flash_description' => $postCategory->name]);
6868
}
6969
}

app/Http/Controllers/Admin/PostController.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,17 @@ public function store(Request $request, PostRepository $postRepository)
6565
'meta_description' => 'nullable|string',
6666
'image' => 'nullable|image',
6767
'category_ids' => 'required|array',
68-
'category_ids.*' => 'exists:post_categories,id'
68+
'category_ids.*' => 'exists:post_categories,id',
69+
'tag_ids' => 'sometimes|required|array',
70+
'tag_ids.*' => 'required|integer|exists:tags,id'
6971
]);
7072

7173
$request->merge(['user_id' => auth()->user()->id]);
7274

7375
$post = Post::create($request->all());
7476
$postRepository->uploadImage($post, $request, 'image');
7577
$post->categories()->sync($request->get('category_ids'));
78+
$post->tags()->sync($request->get('tag_ids'));
7679

7780
return redirect()->route('admin.posts.edit', $post->id)->with(['flash_type' => 'success', 'flash_message' => 'Post created successfully', 'flash_description' => $post->title]);
7881
}
@@ -88,13 +91,16 @@ public function update(Request $request, PostRepository $postRepository, $id)
8891
'meta_description' => 'nullable|string',
8992
'image' => 'nullable|image',
9093
'category_ids' => 'required|array',
91-
'category_ids.*' => 'exists:post_categories,id'
94+
'category_ids.*' => 'exists:post_categories,id',
95+
'tag_ids' => 'sometimes|required|array',
96+
'tag_ids.*' => 'required|integer|exists:tags,id'
9297
]);
9398
$post = Post::findOrFail($id);
9499
$post->fill($request->all());
95100
$post->save();
96101
$postRepository->uploadImage($post, $request, 'image');
97102
$post->categories()->sync($request->get('category_ids'));
103+
$post->tags()->sync($request->get('tag_ids'));
98104
return redirect()->route('admin.posts.edit', $id)->with(['flash_type' => 'success', 'flash_message' => 'Post updated successfully', 'flash_description' => $post->title]);
99105
}
100106
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Admin;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Http\Resources\TagResource;
7+
use App\Models\Tag;
8+
use Illuminate\Http\Request;
9+
use Inertia\Inertia;
10+
11+
class TagController extends Controller
12+
{
13+
14+
public function index(Request $request)
15+
{
16+
$tags = Tag::latest()->paginate($request->get('limit', config('app.pagination_limit')))->withQueryString();
17+
return Inertia::render('Admin/Tags/Tags', ['tags' => TagResource::collection($tags)]);
18+
}
19+
20+
public function store(Request $request)
21+
{
22+
$request->validate([
23+
'name' => 'required|string',
24+
]);
25+
26+
$tag = Tag::create($request->all());
27+
28+
return redirect()->route('admin.tags.index', $tag->id)->with(['flash_type' => 'success', 'flash_message' => 'Tag created successfully', 'flash_description' => $tag->name]);
29+
}
30+
31+
public function update(Request $request, $id)
32+
{
33+
$request->validate([
34+
'name' => 'required|string',
35+
]);
36+
37+
$tag = Tag::findOrFail($id);
38+
$tag->fill($request->all());
39+
$tag->save();
40+
41+
return redirect()->route('admin.tags.index', $tag->id)->with(['flash_type' => 'success', 'flash_message' => 'Tag updated successfully', 'flash_description' => $tag->name]);
42+
}
43+
}

app/Http/Controllers/HomepageController.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,21 @@
33
namespace App\Http\Controllers;
44

55
use App\Models\Page;
6+
use App\Models\Post;
67
use Illuminate\Http\Request;
78
use Inertia\Inertia;
89

910
class HomepageController extends Controller
1011
{
1112
public function __invoke()
1213
{
14+
$post = Post::latest()->first();
15+
16+
//$post->tags()->sync([1]);
17+
1318
$page = Page::slug('home')->firstOrNew();
14-
activity()->withoutLogs(function () use($page) {
19+
20+
activity()->withoutLogs(function () use ($page) {
1521
$page->increment('views');
1622
});
1723

app/Http/Resources/TagResource.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Http\Resources;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Http\Resources\Json\JsonResource;
7+
8+
class TagResource extends JsonResource
9+
{
10+
/**
11+
* Transform the resource into an array.
12+
*
13+
* @return array<string, mixed>
14+
*/
15+
public function toArray(Request $request): array
16+
{
17+
$data = parent::toArray($request);
18+
return $data;
19+
}
20+
}

app/Models/Post.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
use App\Traits\ActivityLoggable;
66
use App\Traits\HasSlug;
7+
use App\Traits\TaggableTrait;
78
use Illuminate\Database\Eloquent\Factories\HasFactory;
89
use Illuminate\Database\Eloquent\Model;
910
use Illuminate\Database\Eloquent\Relations\MorphOne;
1011
use Illuminate\Database\Eloquent\SoftDeletes;
1112

1213
class Post extends Model
1314
{
14-
use HasFactory, ActivityLoggable, SoftDeletes, HasSlug;
15+
use HasFactory, ActivityLoggable, SoftDeletes, HasSlug, TaggableTrait;
1516

1617
protected $guarded = ['id'];
1718

app/Models/Tag.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use App\Traits\ActivityLoggable;
6+
use Illuminate\Database\Eloquent\Factories\HasFactory;
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\SoftDeletes;
9+
10+
class Tag extends Model
11+
{
12+
use HasFactory, SoftDeletes, ActivityLoggable;
13+
14+
protected $guarded = ['id'];
15+
}

app/Models/Taggable.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\Relations\MorphTo;
8+
use Illuminate\Database\Eloquent\SoftDeletes;
9+
10+
class Taggable extends Model
11+
{
12+
use HasFactory, SoftDeletes;
13+
14+
protected $guarded = ['id'];
15+
16+
public function taggable(): MorphTo
17+
{
18+
return $this->morphTo();
19+
}
20+
}

app/Repositories/TagRepository.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Repositories;
4+
5+
use App\Models\Tag;
6+
use Illuminate\Support\Facades\Log;
7+
8+
class TagRepository extends BaseRepository
9+
{
10+
public $model;
11+
12+
function __construct(Tag $model)
13+
{
14+
$this->model = $model;
15+
}
16+
}

app/Traits/TaggableTrait.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Traits;
4+
5+
use App\Models\Tag;
6+
use Illuminate\Database\Eloquent\Relations\MorphTo;
7+
use Illuminate\Database\Eloquent\Relations\MorphToMany;
8+
9+
trait TaggableTrait
10+
{
11+
public function tags(): MorphToMany
12+
{
13+
return $this->morphToMany(Tag::class, 'taggable');
14+
}
15+
}

0 commit comments

Comments
 (0)