Skip to content

Commit eed4eaa

Browse files
committed
create Tags and Comments morphed tables to Post and News models
1 parent 74c6e87 commit eed4eaa

31 files changed

+457
-170
lines changed

app/Comment.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;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class Comment extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $table = 'comments';
13+
14+
protected $guarded = [''];
15+
16+
public function commentable()
17+
{
18+
return $this->morphTo();
19+
}
20+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Comment;
6+
use App\News;
7+
use Illuminate\Http\Request;
8+
9+
class CommentsController extends Controller
10+
{
11+
public function store(Request $request, News $new)
12+
{
13+
$values = $request->validate([
14+
'text' => 'required'
15+
]);
16+
17+
$values['commentable_id'] = $new->id;
18+
$values['commentable_type'] = News::class;
19+
20+
$comment = Comment::create($values);
21+
22+
return back();
23+
}
24+
}

app/Http/Controllers/NewsController.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class NewsController extends Controller
1212
{
1313
public function __construct()
1414
{
15+
$this->middleware('auth');
1516
$this->middleware('role:admin')->except(['index', 'show']);
1617
}
1718

@@ -25,7 +26,8 @@ public function validateRequest($request, $new)
2526

2627
public function index()
2728
{
28-
$news = News::with('tags')->latest()->get();
29+
$news = News::with(['tags', 'comments'])->latest()->get();
30+
2931
return view('news.index', compact('news'));
3032
}
3133

@@ -84,6 +86,6 @@ public function destroy(News $new)
8486

8587
flash( 'New deleted successfully');
8688

87-
return back();
89+
return redirect('/admin/news');
8890
}
8991
}

app/Http/Controllers/PostsController.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function validateRequest($request, $post)
3030

3131
public function index()
3232
{
33-
$posts = auth()->user()->posts()->with('tags')->latest()->get();
33+
$posts = auth()->user()->posts()->with(['tags', 'comments'])->latest()->get();
3434
return view('/posts.index', compact('posts'));
3535
}
3636

@@ -109,6 +109,10 @@ public function destroy(Post $post)
109109
flash( 'Post deleted successfully');
110110
pushNotification('Post deleted successfully', 'New Notification');
111111

112-
return back();
112+
if (auth()->user()->hasRole('admin')) {
113+
return redirect('/admin/posts');
114+
} else {
115+
return back();
116+
}
113117
}
114118
}

app/Http/Controllers/TagsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ public function index(Tag $tag)
1111
{
1212
$posts = $tag->posts()->with('tags')->get();
1313
$news = $tag->news()->with('tags')->get();
14-
return view('index', compact('posts', 'news'));
14+
return view('layouts.tags.index', compact('posts', 'news'));
1515
}
1616
}

app/News.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ class News extends Model
1313

1414
public function tags()
1515
{
16-
return $this->belongsToMany(Tag::class, 'new_tag', 'new_id', 'tag_id');
16+
// return $this->belongsToMany(Tag::class, 'new_tag', 'new_id', 'tag_id');
17+
return $this->morphToMany(Tag::class, 'taggable');
18+
}
19+
20+
public function comments()
21+
{
22+
return $this->morphMany(Comment::class, 'commentable');
1723
}
1824
}

app/Post.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@ class Post extends Model
2020

2121
public function tags()
2222
{
23-
return $this->belongsToMany(Tag::class);
23+
// return $this->belongsToMany(Tag::class);
24+
return $this->morphToMany(Tag::class, 'taggable');
2425
}
2526

2627
public function owner()
2728
{
2829
return $this->belongsTo(User::class);
2930
}
31+
32+
public function comments()
33+
{
34+
return $this->morphMany(Comment::class, 'commentable');
35+
}
3036
}

app/Providers/AppServiceProvider.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace App\Providers;
44

5-
use App\NewTag;
6-
use App\PostTag;
5+
use App\News;
6+
use App\Post;
77
use App\Tag;
8+
use App\Taggable;
89
use Illuminate\Support\ServiceProvider;
910

1011
class AppServiceProvider extends ServiceProvider
@@ -17,13 +18,18 @@ class AppServiceProvider extends ServiceProvider
1718
public function register()
1819
{
1920
view()->composer('layouts.aside-tags', function ($view) {
20-
$postsTags = PostTag::all()->unique('tag_id')->keyBy('tag_id');
21-
$newsTags = NewTag::all()->unique('tag_id')->keyBy('tag_id');
22-
$keys = $postsTags->mergeRecursive($newsTags)->keyBy('tag_id')->keys()->sort();
21+
$postsAndNewsUniqueTagsID = Taggable::where('taggable_type', News::class)
22+
->orWhere('taggable_type', Post::class)
23+
->get()
24+
->unique('tag_id')
25+
->keyBy('tag_id')
26+
->keys()
27+
->sort()
28+
;
2329

24-
$postsAndNewsTags = Tag::all()->whereIn('id', $keys);
30+
$tags = Tag::all()->whereIn('id', $postsAndNewsUniqueTagsID);
2531

26-
$view->with('tagsCloud', $postsAndNewsTags);
32+
$view->with('tagsCloud', $tags);
2733
});
2834
}
2935

app/Tag.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ class Tag extends Model
1515

1616
public function posts()
1717
{
18-
return $this->belongsToMany(Post::class);
18+
// return $this->belongsToMany(Post::class);
19+
return $this->morphedByMany(Post::class, 'taggable');
1920
}
2021

2122
public function news()
2223
{
23-
return $this->belongsToMany(News::class, 'new_tag', 'tag_id', 'new_id');
24+
// return $this->belongsToMany(News::class, 'new_tag', 'tag_id', 'new_id');
25+
return $this->morphedByMany(News::class, 'taggable');
2426
}
2527

2628
public function getRouteKeyName()

app/Taggable.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use Illuminate\Database\Eloquent\Relations\MorphPivot;
6+
7+
class Taggable extends MorphPivot
8+
{
9+
protected $table = 'taggables';
10+
}

0 commit comments

Comments
 (0)