forked from corcel/corcel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostBuilder.php
91 lines (80 loc) · 1.93 KB
/
PostBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* Corcel\PostBuilder
*
* @author Junior Grossi <[email protected]>
*/
namespace Corcel;
use Illuminate\Database\Eloquent\Builder;
class PostBuilder extends Builder
{
/**
* Get only posts with a custom status
*
* @param string $postStatus
* @return \Corcel\PostBuilder
*/
public function status($postStatus)
{
return $this->where('post_status', $postStatus);
}
/**
* Get only published posts
*
* @return \Corcel\PostBuilder
*/
public function published()
{
return $this->status('publish');
}
/**
* Get only posts from a custom post type
*
* @param string $type
* @return \Corcel\PostBuilder
*/
public function type($type)
{
return $this->where('post_type', $type);
}
/**
* Get only posts from an array of custom post types
*
* @param array $type
* @return \Corcel\PostBuilder
*/
public function typeIn(array $type)
{
return $this->whereIn('post_type', $type);
}
public function taxonomy($taxonomy, $term)
{
return $this->whereHas('taxonomies', function($query) use ($taxonomy, $term) {
$query->where('taxonomy', $taxonomy)->whereHas('term', function($query) use ($term) {
$query->where('slug', $term);
});
});
}
/**
* Get only posts with a specific slug
*
* @param string slug
* @return \Corcel\PostBuilder
*/
public function slug($slug)
{
return $this->where('post_name', $slug);
}
/**
* Paginate the results
*
* @param int $perPage
* @param int $currentPage
* @return \Illuminate\Database\Eloquent\Collection
*/
public function paged($perPage = 10, $currentPage = 1)
{
$skip = $currentPage * $perPage - $perPage;
return $this->skip($skip)->take($perPage)->get();
}
}