digikraaft/laravel-posts

为您的 Laravel 应用添加帖子功能

v2.0.0 2023-07-18 15:24 UTC

This package is auto-updated.

Last update: 2024-09-18 18:01:14 UTC


README

GitHub Tests Action Status GitHub Check and fix styling MIT Licensed

为什么选择这个包?

此包为 Laravel 应用提供了简单的博客功能。这是一个有偏见的包。我们在不同的 Laravel 项目中实现了博客功能,并决定将其抽象成一个可以在各个项目中轻松使用的包。如果您觉得它很有用,并且符合您的需求,请尽情使用。欢迎提出改进建议。

依赖项说明

此包使用了以下依赖项。请确保遵循其各自的存储库中的安装和用法说明。

用法

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);

请注意,作者 $author 必须是一个 eloquent 模型,否则将抛出异常 Digikraaft\LaravelPosts\Exceptions\InvalidArgumentException

安装

您可以通过 composer 安装此包

composer require digikraaft/laravel-posts

您必须使用以下命令发布迁移

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

运行迁移以发布帖子表

php artisan migrate

您可以选择使用以下命令发布配置文件

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

将发布到 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',

];

用法

创建帖子

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);

使用属性创建帖子

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);

请注意,作者 $author 必须是一个 eloquent 模型,否则将抛出异常 Digikraaft\LaravelPosts\Exceptions\InvalidArgumentException

所有属性都是可选的。如果您需要添加有关帖子的其他信息,可以使用 meta 属性,如下所示

use Digikraaft\LaravelPosts\Models\Post;


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

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

使用自定义属性创建帖子

如果您需要在帖子中保存特定的信息,可以通过使用自定义属性来实现。

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);

请确保您已将属性添加到 posts 迁移的列中,否则 Laravel 将抛出异常。

检索帖子

帖子模型是一个正常的 eloquent 模型,因此可以使用所有 eloquent 方法和使用查询构建器来检索帖子。

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);

有关检索帖子和使用依赖项的更多方法,请查看以下包的用法说明

检索基本帖子统计信息

您可以获取帖子的阅读时间

use Digikraaft\LaravelPosts\Models\Post;

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

使用别名

此包使用 Laravel Sluggable by Spatie 来处理分类。请查看他们的用法和安装说明。

使用分类

本包使用由 Rinvex 开发的 Laravel 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();

使用标签

本包使用由 Spatie 开发的 Laravel Tags 来处理标签。请检查使用和安装说明。以下是一些使用方法:

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();

更多标签使用方法,请查看文档

事件

当创建帖子时,将触发 Digikraaft\LaravelPosts\Events\PostCreatedEvent 事件。您可以监听此事件并采取必要的操作。帖子实例将被传递到事件类中,并可被访问和使用。

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;
    }
}

自定义模型和迁移

您可以通过扩展 Digikraaft\LaravelPosts\Models\Post 类来更改使用的模型。

您还可以更改在自定义迁移中使用的 dk_posts 表的列名(默认为 model_id)。如果这样做,也请更改 laravel-posts 配置文件中的 model_primary_key_attribute 键。

测试

使用以下命令运行您的测试:

composer test

更多好东西

请访问此处获取更多免费好东西!

变更日志

请查看变更日志以获取最近更改的更多信息。

贡献

有关详细信息,请参阅贡献指南

安全

如果您发现任何安全相关的问题,请通过电子邮件 hello@digikraaft.ng 反馈,而不是使用问题跟踪器。

鸣谢

许可证

MIT 许可证(MIT)。有关更多信息,请参阅许可证文件