Eloquent methods for getting posts by type
Diff
src/Models/Post.php | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
@@ -1,6 +1,7 @@
<?php
namespace Koselig\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
@@ -16,6 +17,29 @@ class Post extends Model
public $primaryKey = 'ID';
/**
* Get all the posts within a certain post type.
*
* @param Builder $query query to add the scope to
* @param string $name name of the post type
* @return Builder
*/
public function scopePostType($query, $name)
{
return $query->where('post_type', $name);
}
/**
* Get all the posts which are published.
*
* @param Builder $query query to add the scope to
* @return Builder
*/
public function scopePublished($query)
{
return $query->where('post_status', 'publish');
}
/**
* Get all the meta values that belong to this post.
*
* @return HasMany