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');
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',
]);
}