Skip to content

Commit b87081e

Browse files
committed
Updates to Relations and Scopes
1 parent 52c1615 commit b87081e

16 files changed

+284
-157
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ Chrisjk123\Blogger\Post::publishedLastWeek()->get();
5151

5252
// Search by unpublished Posts only
5353
Chrisjk123\Blogger\Post::notPublished()->get();
54+
55+
// Search by scheduled Posts only
56+
Chrisjk123\Blogger\Post::scheduled()->get();
57+
58+
// Search by drafted Posts only
59+
Chrisjk123\Blogger\Post::draft()->get();
60+
61+
// Order by latest published
62+
Chrisjk123\Blogger\Post::orderByLatest()->get();
5463
```
5564

5665
## Requirements
@@ -106,6 +115,13 @@ class User extends Authenticatable
106115

107116
// ...
108117
}
118+
119+
120+
// Retrieve the posts created by the user(s)
121+
$user->posts;
122+
123+
// Retrieve the comments created by the user(s)
124+
$user->comments;
109125
```
110126
### Changelog
111127

config/blogs.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
return [
4+
/*
5+
* The default path to the User model in Laravel
6+
*/
7+
'user_class' => \App\User::class,
8+
'user_key_name' => 'id',
9+
];

config/config.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/BloggerFacade.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/BloggerServiceProvider.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,13 @@ public function boot()
2222
$this->publishes([
2323
__DIR__.'/../database/seeds/' => database_path('seeds'),
2424
], 'seeders');
25+
26+
$this->publishes([
27+
__DIR__.'/../config/blogs.php' => config_path('blogs.php'),
28+
], 'config');
2529
}
2630

27-
/**
28-
* Register the application services.
29-
*/
3031
public function register()
3132
{
32-
// Automatically apply the package configuration
33-
$this->mergeConfigFrom(__DIR__.'/../config/config.php', 'blogger');
34-
35-
// Register the main class to use with the facade
36-
$this->app->singleton('blogger', function () {
37-
return new Blogger;
38-
});
3933
}
4034
}

src/Comment.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
namespace Chrisjk123\Blogger;
44

5+
use Chrisjk123\Blogger\Traits\Comment\CommentApproval;
56
use Chrisjk123\Blogger\Traits\Comment\CommentScopes;
7+
use Chrisjk123\Blogger\Traits\IsAuthorable;
68
use Illuminate\Database\Eloquent\Model;
79

810
class Comment extends Model
911
{
10-
use CommentScopes;
12+
use CommentScopes,
13+
CommentApproval,
14+
IsAuthorable;
1115

1216
protected $table = 'comments';
1317

@@ -21,9 +25,4 @@ public function commentable()
2125
{
2226
return $this->morphTo();
2327
}
24-
25-
public function author()
26-
{
27-
return $this->belongsTo(User::class, 'user_id');
28-
}
2928
}

src/Post.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22

33
namespace Chrisjk123\Blogger;
44

5-
use App\User;
5+
use Chrisjk123\Blogger\Traits\IsAuthorable;
66
use Chrisjk123\Blogger\Traits\Post\PostAttributes;
77
use Chrisjk123\Blogger\Traits\Post\PostScopes;
8+
use Chrisjk123\Blogger\Traits\Post\PostsHaveComments;
89
use Illuminate\Database\Eloquent\Model;
910

1011
class Post extends Model
1112
{
12-
use PostScopes, PostAttributes;
13+
use PostScopes,
14+
PostAttributes,
15+
IsAuthorable,
16+
PostsHaveComments;
1317

1418
const PUBLISHED = 'published';
1519
const DRAFT = 'draft';
@@ -41,9 +45,4 @@ public function tags()
4145
{
4246
return $this->morphToMany(Tag::class, 'taggable');
4347
}
44-
45-
public function author()
46-
{
47-
return $this->belongsTo(User::class, 'id', 'user_id');
48-
}
4948
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Chrisjk123\Blogger\Traits\Comment;
4+
5+
trait CommentApproval
6+
{
7+
public function scopeApproved($query)
8+
{
9+
return $query->where('is_approved', true);
10+
}
11+
12+
public function scopeUnapproved($query)
13+
{
14+
return $query->where('is_approved', false);
15+
}
16+
17+
public function approve()
18+
{
19+
$this->update(['is_approved' => true]);
20+
21+
return $this;
22+
}
23+
24+
public function disapprove()
25+
{
26+
$this->update(['is_approved' => false]);
27+
28+
return $this;
29+
}
30+
}

src/Traits/IsAuthorable.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Chrisjk123\Blogger\Traits;
4+
5+
trait IsAuthorable
6+
{
7+
public function author()
8+
{
9+
return $this->belongsTo(
10+
config('blogs.user_class'),
11+
'user_id',
12+
config('blogs.user_key_name')
13+
);
14+
}
15+
16+
public function user()
17+
{
18+
return $this->belongsTo(
19+
config('blogs.user_class'),
20+
'user_id',
21+
config('blogs.user_key_name')
22+
);
23+
}
24+
}

src/Traits/Post/PostAttributes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public function isPublished()
1414
return $this->status == self::PUBLISHED;
1515
}
1616

17-
public function notPublished()
17+
public function isNotPublished()
1818
{
1919
return $this->status == self::DRAFT
2020
|| $this->status == self::SCHEDULED;

0 commit comments

Comments
 (0)