PHPackages                             digikraaft/laravel-posts - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. digikraaft/laravel-posts

ActiveLibrary

digikraaft/laravel-posts
========================

Add posts functionality to your laravel app

v2.0.0(2y ago)114MITPHPPHP ^8.1

Since Sep 20Pushed 2y agoCompare

[ Source](https://github.com/digikraaft/laravel-posts)[ Packagist](https://packagist.org/packages/digikraaft/laravel-posts)[ Docs](https://github.com/digikraaft/laravel-posts)[ RSS](/packages/digikraaft-laravel-posts/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (9)Versions (3)Used By (0)

Add basic Blog/Post functionality to your Laravel app.
======================================================

[](#add-basic-blogpost-functionality-to-your-laravel-app)

[![GitHub Tests Action Status](https://camo.githubusercontent.com/c781276d461160bc957251ec7732eeb6c0385c9d88daf9312418ee5ff02b87a9/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f646967696b72616166742f6c61726176656c2d706f7374732f74657374733f6c6162656c3d7465737473)](https://github.com/digikraaft/laravel-posts/actions?query=workflow%3Atests)[![GitHub Check and fix styling](https://github.com/digikraaft/laravel-posts/workflows/Check%20&%20fix%20styling/badge.svg)](https://github.com/digikraaft/laravel-posts/actions?query=workflow%3A%22Check+%26+fix+styling%22)[![MIT Licensed](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

Why this package?
-----------------

[](#why-this-package)

This package provides a simple blog functionality for use in a Laravel app. This is an opinionated package. We have had to implement blog functionalities in different Laravel projects and decided to abstract this into a package which can be easily used across our projects. If you find it useful and meets your needs, by all means please use it. Suggested improvements are welcome.

Notes on dependencies
---------------------

[](#notes-on-dependencies)

This package uses the following dependencies. Please ensure to follow the installation and usage instructions from their respective repositories:

- [Laravel Categories](https://github.com/rinvex/laravel-categories) by [Rinvex](https://github.com/rinvex)
- [Laravel Sluggagle](https://github.com/spatie/laravel-sluggable) by [Spatie](https://github.com/spatie)
- [Laravel Translatable](https://github.com/spatie/laravel-translatable) by [Spatie](https://github.com/spatie)
- [Laravel ActivityLog](https://github.com/spatie/laravel-activitylog) by [Spatie](https://github.com/spatie)
- [Laravel Model Status](https://github.com/spatie/laravel-model-status) by [Spatie](https://github.com/spatie)
- [Laravel Tags](https://github.com/spatie/laravel-tags) by [Spatie](https://github.com/spatie)

Usage
-----

[](#usage)

```
use Digikraaft\LaravelPosts\Models\Post;

// Create a post
$title = 'My first Post';
$content = 'Not really sure of what to write here! Can I get some help please?';
Post::create($title, $content);

// Create post with more attributes
$title = 'My Second Post';
$content = 'I may just need to get the services of a content writer. Thoughts?';
$author = User::find(1);
$additionalDetails = [
    'author' => $author,
    'created_at' => \Illuminate\Support\Carbon::now(),
    'published_at' => \Illuminate\Support\Carbon::now(),
];
$post = Post::create($title, $content, $additionalDetails);
```

Please note that the author `$author` must be an eloquent model otherwise an exception `Digikraaft\LaravelPosts\Exceptions\InvalidArgumentException` will be thrown.

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require digikraaft/laravel-posts
```

You must publish the migration with:

```
php artisan vendor:publish --provider="Digikraaft\LaravelPosts\LaravelPostsServiceProvider" --tag="migrations"
```

Run the migration to publish the posts table with:

```
php artisan migrate
```

You can optionally publish the config-file with:

```
php artisan vendor:publish --provider="Digikraaft\LaravelPosts\LaravelPostsServiceProvider" --tag="config"
```

The content of the file that will be published to `config/laravel-posts.php`:

```
return [
    /*
     * The name of the column which holds the ID of the model that is the author of the posts.
     *
     * Only change this value if you have set a different name in the migration for the posts table.
     */
    'model_primary_key_attribute' => 'model_id',

    /*
     * The table name where your posts will be stored.
     */
    'posts_table_name' => 'dk_posts',

    /*
     * The column name where posts slug should be generated from
     */
    'generate_slug_from' => 'title',

     /*
     * The column name where slugs should be saved to
     */
    'save_slug_to' => 'slug',

];
```

Usage
-----

[](#usage-1)

### Create a post

[](#create-a-post)

```
use Digikraaft\LaravelPosts\Models\Post;

// create a post
$title = 'My first Post';
$content = 'Not really sure of what to write here! Can I get some help please?';
Post::create($title, $content);
```

### Create post with attributes

[](#create-post-with-attributes)

```
use Digikraaft\LaravelPosts\Models\Post;

// create post with more attributes
$title = 'My Second Post';
$content = 'I may just need to get the services of a content writer. Thoughts?';
$author = User::find(1);
$additionalDetails = [
    'author' => $author,
    'updated_at' => \Illuminate\Support\Carbon::now(),
    'created_at' => \Illuminate\Support\Carbon::now(),
    'published_at' => \Illuminate\Support\Carbon::now(),
];
$post = Post::create($title, $content, $additionalDetails);
```

Please note that the author `$author` must be an eloquent model otherwise an exception `Digikraaft\LaravelPosts\Exceptions\InvalidArgumentException` will be thrown.

All attributes are optional. If you need to add additional information about a post, you can use the `meta` attribute this way:

```
use Digikraaft\LaravelPosts\Models\Post;

$title = "Post title";
$content = "Post content";

$additionalDetails = [
    'meta' => [
        'seo_title' => 'SEO Title'
    ]
];
Post::create($title, $content, $additionalDetails);
```

### Create post with custom attributes

[](#create-post-with-custom-attributes)

If you need to save specific information on a post, you can do it by using custom attributes.

```
use Digikraaft\LaravelPosts\Models\Post;

// Create post with custom attributes
$title = 'My Third Post';
$content = 'At this point, it amazes me why I can\'t seem to find the right words!';
$author = User::find(1);
$customDetails = [
    'custom_key' => $author,
];
$post = Post::create($title, $content, $customDetails);
```

Please ensure you have added the attributes as columns to the `posts` migration otherwise, Laravel will throw an exception.

### Retrieving Posts

[](#retrieving-posts)

The Post model is a normal eloquent model so all eloquent methods and query builders can be used in retrieving posts.

```
use Digikraaft\LaravelPosts\Models\Post;

//Retrieve post by id
Post::find(1);

//Retrieve post by slug
Post::where('slug', 'post-title-1')->get();

//Retrieve post instance by slug
$post = Post::whereSlug('post-title-1');

//Retrieve all published posts. Published posts are posts where published_at date is today or in the past
Post::published();

//Retrieve all published posts within a period
$from = now()->subMonth();
$to = now();
Post::published($from, $to);
//Note that an `InvalidDate` exception will be thrown if the $from date is later than the $to

//Retrieve all scheduled posts. Scheduled posts are posts with published_at date in the future
Post::scheduled();

//Retrieve all scheduled posts within a period
$from = now();
$to = now()->addMonth();
Post::scheduled($from, $to);

//retrieve all posts by author
$author = User::find(1);
Post::byAuthor($author);
```

For more ways to retrieve posts and use the dependencies, check usage instructions of the following packages:

- [Posts Categories](https://github.com/rinvex/laravel-categories)
- [Posts Translations](https://github.com/spatie/laravel-translatable)
- [Laravel ActivityLog](https://github.com/spatie/laravel-activitylog)
- [Post Status](https://github.com/spatie/laravel-model-status)
- [Post Tags](https://github.com/spatie/laravel-tags)
- [Post Slug](https://github.com/spatie/laravel-sluggable)

#### Retrieving basic Post Stats

[](#retrieving-basic-post-stats)

You can get the reading time of a post:

```
use Digikraaft\LaravelPosts\Models\Post;

$post = Post::find(1);
$post->readingTime(); // returns reading time in minutes
```

### Using Slug

[](#using-slug)

This package uses [Laravel Sluggable](https://github.com/spatie/laravel-sluggable) by [Spatie](https://github.com/spatie) to handle categories. Please check their usage and installation instructions.

### Using Categories

[](#using-categories)

This package uses [Laravel Categories](https://github.com/rinvex/laravel-categories) by [Rinvex](https://github.com/rinvex) to handle categories. Please check the usage and installation instructions. This package however has helper classes to use categories:

```
use Digikraaft\LaravelPosts\Models\Post;
use Digikraaft\LaravelPosts\Models\PostCategory;

//create categories
$attributes = ['name' => 'News', 'slug' => 'news'];
PostCategory::create($attributes);

//attach categories
$post = Post::find(1);
$post->attachCategories(['first-category', 'second-category']);

// Get attached categories collection
$post->categories;

// Get attached categories query builder
$post->categories();
```

### Using Tags

[](#using-tags)

This package uses [Laravel Tags](https://github.com/spatie/laravel-tags) by [Spatie](https://github.com/spatie) to handle tags. Please check the usage and installation instructions. Here are a few ways to use:

```
use Digikraaft\LaravelPosts\Models\Post;

//attach tags
$post = Post::find(1);

//attach single tag
$post->attachTag('first tag');

//multiple tags
$tags = ['second tag', 'third tag', 'fourth tag', 'fifth tag'];
$post->attachTags($tags);

$post->attachTags(['sixth_tag','seventh_tag'],'some_type');

// detaching tags
$post->detachTags('third tag');
$post->detachTags(['fourth tag', 'fifth tag']);

// Get all post tags
$post->tags;

// retrieving tags with a type
$post->tagsWithType('some_type');

// syncing tags
$post->syncTags(['first tag', 'second tag']); // all other tags on this model will be detached

// retrieving post that have any of the given tags
Post::withAnyTags(['first tag', 'second tag'])->get();

// retrieve posts that have all of the given tags
Post::withAllTags(['first tag', 'second tag'])->get();
```

For more tag usage, checkout the [documentation](https://github.com/spatie/laravel-tags)

### Events

[](#events)

The `Digikraaft\LaravelPosts\Events\PostCreatedEvent` event will be dispatched when a post has been created. You can listen to this event and take necessary actions. An instance of the post will be passed to the event class and can be accessed for use:

```
namespace Digikraaft\LaravelPosts\Events;

use Digikraaft\LaravelPosts\Models\Post;

class PostCreatedEvent
{
    /** @var \Digikraaft\LaravelPosts\Models\Post */
    public Post $post;

    public function __construct(Post $post)
    {
        $this->post = $post;
    }
}
```

### Custom model and migration

[](#custom-model-and-migration)

You can change the model used by extending the `Digikraaft\LaravelPosts\Models\Post` class.

You can also change the column name used in the `dk_posts` table (default is `model_id`) when using a custom migration. If this is the case, also change the `model_primary_key_attribute` key of the `laravel-posts` config file.

Testing
-------

[](#testing)

Use the command below to run your tests:

```
composer test
```

More Good Stuff
---------------

[](#more-good-stuff)

Check [here](https://github.com/digikraaft) for more awesome free stuff!

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Tim Oladoyinbo](https://github.com/timoladoyinbo)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity56

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~301 days

Total

2

Last Release

1026d ago

Major Versions

v1.0.0 → v2.0.02023-07-18

### Community

Maintainers

![](https://www.gravatar.com/avatar/298ea582e7fdad689b46e4fa771f3eb5caa963a9a7a76b2416dd98bada23d15f?d=identicon)[digikraaft](/maintainers/digikraaft)

---

Top Contributors

[![timoladoyinbo](https://avatars.githubusercontent.com/u/66416657?v=4)](https://github.com/timoladoyinbo "timoladoyinbo (24 commits)")

---

Tags

laravelblogpostsdigikraaft

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/digikraaft-laravel-posts/health.svg)

```
[![Health](https://phpackages.com/badges/digikraaft-laravel-posts/health.svg)](https://phpackages.com/packages/digikraaft-laravel-posts)
```

###  Alternatives

[io238/laravel-iso-countries

Ready-to-use Laravel models and relations for country (ISO 3166), language (ISO 639-1), and currency (ISO 4217) information with multi-language support.

5462.3k](/packages/io238-laravel-iso-countries)[bjuppa/laravel-blog

Add blog functionality to your Laravel project

483.3k1](/packages/bjuppa-laravel-blog)[jacobtims/filament-logger

Activity logger for filament

1721.0k4](/packages/jacobtims-filament-logger)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
