PHP code example of digikraaft / laravel-posts

1. Go to this page and download the library: Download digikraaft/laravel-posts library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

digikraaft / laravel-posts example snippets


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

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

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

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

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

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

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;
    }
}
bash
php artisan vendor:publish --provider="Digikraaft\LaravelPosts\LaravelPostsServiceProvider" --tag="migrations"
bash
php artisan migrate
bash
php artisan vendor:publish --provider="Digikraaft\LaravelPosts\LaravelPostsServiceProvider" --tag="config"