PHP code example of plank / before-and-after-model-events
1. Go to this page and download the library: Download plank/before-and-after-model-events 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/ */
plank / before-and-after-model-events example snippets
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Plank\BeforeAndAfterModelEvents\Concerns\BeforeAndAfterEvents;
class User extends Model
{
use BeforeAndAfterEvents;
protected $fillable = ['name', 'email'];
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Plank\BeforeAndAfterModelEvents\Concerns\BeforeAndAfterEvents;
class Post extends Model
{
use BeforeAndAfterEvents;
protected $fillable = ['title', 'content', 'status'];
}
use App\Models\Post;
// Standard Laravel events
Post::beforeEvent('creating', function ($post) {
$post->slug = Str::slug($post->title);
$post->status = 'draft';
});
Post::afterEvent('created', function ($post) {
Cache::tags(['posts'])->flush();
Log::info("New post created: {$post->title}");
});
Post::beforeEvent('updating', function ($post) {
if ($post->isDirty('title')) {
$post->slug = Str::slug($post->title);
}
});
Post::afterEvent('deleted', function ($post) {
Storage::delete($post->image_path);
});
Post::beforeEvent('deleting', function ($post) {
if ($post->is_protected) {
// Prevent deletion of protected posts
return false;
}
});
// This will fail silently if the post is protected
$post->delete(); // Returns false, post not deleted
class Post extends Model
{
use BeforeAndAfterEvents;
public function publish()
{
// Fire custom event with before/after support
if ($this->fireModelEvent('publishing') === false) {
return false;
}
$this->status = 'published';
$this->published_at = now();
$this->save();
return true;
}
}
// Register listeners for the custom event
Post::beforeEvent('publishing', function ($post) {
if (!$post->isReadyForPublishing()) {
return false; // Prevent publishing
}
$post->seo_title = $post->seo_title ?: $post->title;
});
Post::afterEvent('publishing', function ($post) {
Mail::to($post->author)->send(new PostPublishedNotification($post));
Cache::tags(['published-posts'])->flush();
});
Post::beforeEvent('creating', function ($post) {
$post->author_id = auth()->id();
});
Post::beforeEvent('creating', function ($post) {
$post->reading_time = $this->calculateReadingTime($post->content);
});
Post::beforeEvent('creating', function ($post) {
if (!$post->excerpt) {
$post->excerpt = Str::limit(strip_tags($post->content), 150);
}
});
use Illuminate\Database\Eloquent\SoftDeletes;
class Post extends Model
{
use BeforeAndAfterEvents, SoftDeletes;
}
// Handle soft delete events
Post::beforeEvent('deleting', function ($post) {
$post->deleted_by = auth()->id();
});
// Handle restoration events
Post::beforeEvent('restoring', function ($post) {
Log::info("Restoring post: {$post->title}");
});
Post::afterEvent('restored', function ($post) {
Cache::forget("deleted_posts");
});
class Post extends Model
{
use BeforeAndAfterEvents;
// Existing Laravel event dispatchers still work
protected $dispatchesEvents = [
'saved' => PostSavedEvent::class,
];
}
// Both systems work together
Post::beforeEvent('saving', function ($post) {
// Runs before the 'saving' event and PostSavedEvent
});
// These all work automatically
Post::beforeEvent('publishing', $callback);
Post::beforeEvent('archiving', $callback);
Post::beforeEvent('featuring', $callback);
Post::beforeEvent('customBusinessLogic', $callback);
bash
composer analyse
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.