Skip to content

Commit 5c8f9b4

Browse files
committed
Tweaks to the comment functionality and docs
1 parent a74d0d6 commit 5c8f9b4

File tree

11 files changed

+67
-32
lines changed

11 files changed

+67
-32
lines changed

README.md

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ You can install the package via composer:
8080
composer require chrisjk123/laravel-blogger
8181
```
8282

83-
You can publish the migrations with:
83+
Publish the migrations with:
8484

8585
```bash
8686
php artisan vendor:publish --provider="Chriscreates\Blog\BloggerServiceProvider" --tag="migrations"
@@ -94,21 +94,34 @@ php artisan vendor:publish --provider="Chriscreates\Blog\BloggerServiceProvider"
9494
```
9595

9696
This is the contents of the published config file, if your `User` class is
97-
within a different directory, you can change it here as well as the User primary key:
97+
within a different directory or has a different primary key it can be changed here.
98+
99+
Furthermore, the settings for allowing commenting on posts is set here, the default
100+
is set to true as well as guest commenting.
98101

99102
```php
100103
return [
101104
/*
102-
* The default path to the User model in Laravel
105+
* User: the default path to the User model in Laravel and primary key
106+
*/
107+
'user' => [
108+
'user_class' => \App\User::class,
109+
'user_key_name' => 'id',
110+
],
111+
112+
/*
113+
* Post: allow commenting on posts / allow guest commenting on posts
103114
*/
104-
'user_class' => \App\User::class,
105-
'user_key_name' => 'id',
115+
'posts' => [
116+
'allow_comments' => true,
117+
'allow_guest_comments' => true,
118+
],
106119
];
107120
```
108121

109122
## Local testing
110123

111-
You can publish the factories with:
124+
For testing purposes, you can publish the factories with:
112125

113126
```bash
114127
php artisan vendor:publish --provider="Chriscreates\Blog\BloggerServiceProvider" --tag="factories"
@@ -136,7 +149,7 @@ class User extends Authenticatable
136149
// Retrieve the posts created by the user(s)
137150
$user->posts;
138151

139-
// Retrieve the comments created by the user(s)
152+
// Retrieve the comments created by the guest/user(s)
140153
$user->comments;
141154
```
142155

config/blogs.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
return [
44
/*
5-
* The default path to the User model in Laravel
5+
* User: the default path to the User model in Laravel and primary key
66
*/
7-
'user_class' => \App\User::class,
8-
'user_key_name' => 'id',
7+
'user' => [
8+
'user_class' => \App\User::class,
9+
'user_key_name' => 'id',
10+
],
911

1012
/*
11-
* The default path to the User model in Laravel
13+
* Post: allow commenting on posts / allow guest commenting on posts
1214
*/
1315
'posts' => [
1416
'allow_comments' => true,

database/factories/CategoryFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Chriscreatess\Blog\Category;
3+
use Chriscreates\Blog\Category;
44
use Faker\Generator as Faker;
55
use Illuminate\Support\Str;
66

database/factories/CommentFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Chriscreatess\Blog\Comment;
3+
use Chriscreates\Blog\Comment;
44
use Faker\Generator as Faker;
55

66
$factory->define(Comment::class, function (Faker $faker) {

database/factories/PostFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

3-
use Chriscreatess\Blog\Category;
4-
use Chriscreatess\Blog\Post;
3+
use Chriscreates\Blog\Category;
4+
use Chriscreates\Blog\Post;
55
use Faker\Generator as Faker;
66
use Illuminate\Support\Str;
77

@@ -10,7 +10,7 @@
1010

1111
return [
1212
'category_id' => factory(Category::class)->create()->id,
13-
'name' => $title,
13+
'title' => $title,
1414
'slug' => Str::slug($title),
1515
'excerpt' => $faker->name,
1616
'content' => $faker->paragraph,

database/factories/TagFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
use Chriscreatess\Blog\Tag;
3+
use Chriscreates\Blog\Tag;
44
use Faker\Generator as Faker;
55

66
$factory->define(Tag::class, function (Faker $faker) {

database/migrations/2019_12_28_101010_create_posts_table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function up()
2020
$table->integer('user_id')->nullable();
2121
$table->index('user_id');
2222
$table->string('title');
23-
$table->string('sub_title');
23+
$table->string('sub_title')->nullable();
2424
$table->string('slug')->nullable();
2525
$table->string('excerpt')->nullable();
2626
$table->text('content')->nullable();

database/seeds/PostsTableSeeder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
use Chriscreatess\Blog\Comment;
4-
use Chriscreatess\Blog\Post;
5-
use Chriscreatess\Blog\Tag;
3+
use Chriscreates\Blog\Comment;
4+
use Chriscreates\Blog\Post;
5+
use Chriscreates\Blog\Tag;
66
use Illuminate\Database\Seeder;
77

88
class PostsTableSeeder extends Seeder

src/Post.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ public function category()
4040

4141
public function comments()
4242
{
43+
if ( ! $this->allow_comments) {
44+
return null;
45+
}
46+
47+
if ( ! $this->allow_guest_comments) {
48+
return $this->morphMany(Comment::class, 'commentable')
49+
->whereNull('user_id');
50+
}
51+
4352
return $this->morphMany(Comment::class, 'commentable');
4453
}
4554

@@ -53,18 +62,19 @@ public function disapprovedComments()
5362
return $this->comments()->where('is_approved', false);
5463
}
5564

56-
public function tags()
65+
public function guestComments()
5766
{
58-
return $this->morphToMany(Tag::class, 'taggable');
67+
return $this->comments()->whereNull('user_id');
5968
}
6069

61-
public static function boot()
70+
public function userComments()
6271
{
63-
parent::boot();
72+
return $this->comments()->whereNotNull('user_id');
73+
}
6474

65-
static::deleted(function (Model $post) {
66-
$post->tags()->detach();
67-
});
75+
public function tags()
76+
{
77+
return $this->morphToMany(Tag::class, 'taggable');
6878
}
6979

7080
public function path()

src/Traits/IsAuthorable.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ trait IsAuthorable
99
public function author()
1010
{
1111
return $this->belongsTo(
12-
config('blogs.user_class'),
12+
config('blogs.user.user_class'),
1313
'user_id',
14-
config('blogs.user_key_name')
14+
config('blogs.user.user_key_name')
1515
);
1616
}
1717

1818
public function user()
1919
{
2020
return $this->belongsTo(
21-
config('blogs.user_class'),
21+
config('blogs.user.user_class'),
2222
'user_id',
23-
config('blogs.user_key_name')
23+
config('blogs.user.user_key_name')
2424
);
2525
}
2626

0 commit comments

Comments
 (0)