Skip to content

Commit c641339

Browse files
committed
Post scopes and updated readme
1 parent ba25e97 commit c641339

File tree

5 files changed

+105
-24
lines changed

5 files changed

+105
-24
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,51 @@
88

99
This package offers fully blog database tables as well as preset models with relations, scopes and seeders.
1010

11+
Here are some code examples:
12+
13+
```php
14+
// Search by the short whereCategories method OR use whereCategory() and specify the field
15+
$results = Chrisjk123\Blogger\Post::whereCategories($categories = null)->get();
16+
$results = Chrisjk123\Blogger\Post::whereCategory($field, $operator, $value)->get();
17+
18+
// Search by Category ID OR IDs
19+
$results = Chrisjk123\Blogger\Post::whereCategories(1)->get();
20+
$results = Chrisjk123\Blogger\Post::whereCategory('id', 1)->get();
21+
----------
22+
$results = Chrisjk123\Blogger\Post::whereCategories([3, 6, 7])->get();
23+
$results = Chrisjk123\Blogger\Post::whereCategory('id', [3, 6, 7])->get();
24+
25+
// Search by Category name OR names
26+
$results = Chrisjk123\Blogger\Post::whereCategories('Izabella Bins II')->get();
27+
$results = Chrisjk123\Blogger\Post::whereCategory('name', 'Izabella Bins II')->get();
28+
----------
29+
$results = Chrisjk123\Blogger\Post::whereCategories(['Izabella Bins II', 'Osborne Fay'])->get();
30+
$results = Chrisjk123\Blogger\Post::whereCategory('name', ['Izabella Bins II', 'Osborne Fay'])->get();
31+
32+
// Search by Category model or a Category Collection
33+
$category = Chrisjk123\Blogger\Category::where('id', 7)->first();
34+
$results = Chrisjk123\Blogger\Post::whereCategories($category)->get();
35+
----------
36+
$categories = Chrisjk123\Blogger\Category::whereIn('id', [3, 6, 7])->get();
37+
$results = Chrisjk123\Blogger\Post::whereCategories($categories)->get();
38+
39+
// Search by related Post (tags or category)
40+
$post = Chrisjk123\Blogger\Post::find(8);
41+
$results = Chrisjk123\Blogger\Post::relatedByPostTags($post)->get();
42+
----------
43+
$results = Chrisjk123\Blogger\Post::relatedByPostCategory($post)->get();
44+
45+
// Search by published Posts only
46+
Chrisjk123\Blogger\Post::published()->get();
47+
----------
48+
Chrisjk123\Blogger\Post::publishedLastMonth()->get();
49+
----------
50+
Chrisjk123\Blogger\Post::publishedLastWeek()->get();
51+
52+
// Search by unpublished Posts only
53+
Chrisjk123\Blogger\Post::notPublished()->get();
54+
```
55+
1156
## Requirements
1257

1358
This package requires Laravel 5.8 or higher, PHP 7.2 or higher and a database that supports json fields and MySQL compatible functions.

src/BloggerFacade.php

100755100644
File mode changed.

src/BloggerServiceProvider.php

100755100644
File mode changed.

src/Post.php

100755100644
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Post extends Model
2929

3030
public function category()
3131
{
32-
return $this->hasOne(Category::class, 'category_id');
32+
return $this->hasOne(Category::class, 'id', 'category_id');
3333
}
3434

3535
public function comments()
@@ -44,6 +44,6 @@ public function tags()
4444

4545
public function author()
4646
{
47-
return $this->belongsTo(User::class, 'user_id');
47+
return $this->belongsTo(User::class, 'id', 'user_id');
4848
}
4949
}

src/Traits/Post/PostScopes.php

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,85 @@ trait PostScopes
1212
{
1313
public function scopeWhereCategories(Builder $query, $categories = null)
1414
{
15-
if (is_null($categories)) {
16-
return $query;
17-
}
18-
15+
// search by category name
1916
if (is_string($categories)) {
20-
return $this->whereCategory($categories, 'name');
17+
return $this->whereCategory('name', $categories);
2118
}
2219

20+
// search by category id
2321
if (is_int($categories)) {
24-
return $this->whereCategory($categories, 'id');
22+
return $this->whereCategory('id', $categories);
2523
}
2624

25+
// search by multiple categories
2726
if (is_array($categories)) {
28-
return $query->with('category')
29-
->whereHas('category', function (Builder $query) use ($categories) {
30-
return $query->whereIn('id', $categories);
31-
});
27+
if (is_int($categories[0])) {
28+
$field = 'id';
29+
} else {
30+
$field = 'name';
31+
}
32+
33+
return $this->whereCategory($field, $categories);
3234
}
3335

36+
// search by category model
3437
if ($categories instanceof Category) {
35-
return $this->whereCategory($categories->id, 'id');
38+
return $this->whereCategory('id', $categories->id);
3639
}
3740

41+
// search by categories collection
3842
if ($categories instanceof Collection) {
39-
return $query->with('category')
40-
->whereHas('category', function (Builder $query) use ($categories) {
41-
return $query->whereIn(
42-
'id',
43-
$categories->pluck('id')->toArray()
44-
);
45-
});
43+
return $this->whereCategory('id', $categories->pluck('id')->toArray());
4644
}
4745

4846
return $query;
4947
}
5048

51-
public function scopeWhereCategory(Builder $query, $category, $type = 'name')
49+
public function scopeWhereCategory(Builder $query, ...$options)
5250
{
51+
$collection = collect([
52+
'field' => 'id',
53+
'operator' => '=',
54+
'value' => null,
55+
]);
56+
57+
// Search by field and value
58+
if (count($options) == 2) {
59+
$collection = $collection->replace(['field' => $options[0]])
60+
->replace(['value' => $options[1]]);
61+
}
62+
63+
// Search by field, operator and value
64+
if (count($options) == 3) {
65+
$collection = $collection->replace(['field' => $options[0]])
66+
->replace(['operator' => $options[1]])
67+
->replace(['value' => $options[2]]);
68+
}
69+
5370
$query->with('category');
5471

55-
return $query->whereHas('category', function (Builder $query) use ($type, $category) {
56-
return $query->where($type, $category);
57-
});
72+
if (is_array($collection['value'])) {
73+
return $query->whereHas(
74+
'category',
75+
function (Builder $query) use ($collection) {
76+
return $query->whereIn(
77+
$collection['field'],
78+
$collection['value']
79+
);
80+
}
81+
);
82+
}
83+
84+
return $query->whereHas(
85+
'category',
86+
function (Builder $query) use ($collection) {
87+
return $query->where(
88+
$collection['field'],
89+
$collection['operator'],
90+
$collection['value']
91+
);
92+
}
93+
);
5894
}
5995

6096
public function scopeRelatedByPostTags(Builder $query, Post $post)

0 commit comments

Comments
 (0)