PHP code example of mnaimjons / laravel-stories

1. Go to this page and download the library: Download mnaimjons/laravel-stories 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/ */

    

mnaimjons / laravel-stories example snippets


return [
    'default_expiration' => 24, // Story lifetime in hours
];



namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Mnaimjons\LaravelStories\Contracts\HasStories;
use Mnaimjons\LaravelStories\Traits\HasStoriesTrait;

class User extends Authenticatable implements HasStories
{
    use HasStoriesTrait;

    // ...
}

$user = auth()->user();

$story = $user->stories()->create([
    'title' => 'My Story',
    'content' => 'This is my first story.',
    'expires_at' => now()->addHours(24), // Optional, defaults to 24 hours
]);

// Adding a media file (image or video)
$story
    ->addMediaFromRequest('media') // 'media' is the name of the file input field
    ->toMediaCollection('stories');

$stories = $user->stories()
    ->where('expires_at', '>', now())
    ->with(['media', 'views', 'comments', 'reactions'])
    ->get();

$story = Story::find($storyId);

$story->comments()->create([
    'user_id' => auth()->id(),
    'comment' => 'Great story!',
]);

public function comment(Request $request, $id)
{
    $request->validate([
        'comment' => '     'user_id' => $request->user()->id,
        'comment' => $request->input('comment'),
    ]);

    return response()->json($comment, 201);
}

$story = Story::find($storyId);

$story->reactions()->create([
    'user_id' => auth()->id(),
    'reaction' => '👍', // Or any other reaction type
]);

$story = Story::find($storyId);

$story->views()->firstOrCreate([
    'user_id' => auth()->id(),
]);

public function viewStory(Request $request, $id)
{
    $story = Story::with('storyable')->findOrFail($id);

    // Record the view
    if ($request->user()) {
        $story->views()->firstOrCreate([
            'user_id' => $request->user()->id,
        ]);
    }

    return response()->json($story);
}

protected function schedule(Schedule $schedule)
{
    $schedule->command('stories:cleanup')->hourly();
}

'default_expiration' => 48, // Story lifetime in hours

public function test_user_can_create_story()
{
    $user = User::factory()->create();

    $this->actingAs($user)
        ->post('/stories', [
            'storyable_type' => 'App\\Models\\User',
            'storyable_id' => $user->id,
            'title' => 'Test Story',
            'content' => 'Test story content',
        ])
        ->assertStatus(201)
        ->assertJson([
            'title' => 'Test Story',
            'content' => 'Test story content',
        ]);
}
bash
   php artisan vendor:publish --provider="Mnaimjons\LaravelStories\LaravelStoriesServiceProvider" --tag="migrations"
   php artisan migrate
   
bash
   php artisan vendor:publish --provider="Mnaimjons\LaravelStories\LaravelStoriesServiceProvider" --tag="config"
   
bash
   composer endor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="migrations"
   php artisan migrate