Laravel Model Creating Event Example - TechvBlogs

Laravel Model Creating Event Example

Explore the magic of Laravel's 'creating' events! Our easy guide shows how to automate tasks step by step.


Smit Pipaliya - Author - TechvBlogs
Smit Pipaliya
 

4 months ago

TechvBlogs - Google News

In the enchanting world of Laravel, model events wield incredible power, offering a gateway to execute tailored actions when a model undergoes creation, updates, or deletions. This tutorial delves into the wizardry of the 'creating' event, unraveling its potential for automating processes within a Laravel model. Join me on this journey as we explore the nuances of leveraging the creating event to enhance the functionality of your Laravel applications.

Understanding the Creating Event

Model events in Laravel are like spells, allowing developers to weave magic into the fabric of their applications. Imagine you have a 'Post' model, and you desire to bestow upon it a unique slug before its creation. Behold the creating event, a mystical tool that can effortlessly achieve this feat.

Laravel Model Creating Event Example

Creating the Post Model

To embark on this magical quest, let's conjure the 'Post' model using Laravel's Artisan command:

php artisan make:model Post

Now, let's infuse the 'creating' event into our 'Post' model:

// app/Models/Post.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

class Post extends Model
{
    protected $fillable = ['title', 'content', 'slug'];

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($post) {
            info('Creating event call: '.$post);
   
            $post->slug = str()->slug($post->title);
        });
  
        static::created(function ($post) {
        	info('Created event call: '.$post);
        });
    }
}

In this incantation, the creating event, when triggered, gracefully sets the post's slug based on its title using Laravel's str()->slug() method.

Creating a Post Record: Let's manifest a new 'Post' instance and witness the magic unfold:

// app/Http/Controllers/PostController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Post;

class PostController extends Controller
{
    public function index(Request $request)
    {
        $post = Post::create([
            'title' => 'Enchanting Laravel',
            'content' => 'Unleashing the power of Laravel model events.'
        ]);

        dd($post);
    }
}

Execute the controller code, and behold the enchanting logs:

[2023-10-20 14:37:26] local.INFO: Creating event call: {"title":"Enchanting Laravel","content":"Unleashing the power of Laravel model events."}
[2023-10-20 14:37:26] local.INFO: Created event call: {"title":"Enchanting Laravel","content":"Unleashing the power of Laravel model events.","slug":"enchanting-laravel","updated_at":"2023-10-20T14:37:26.000000Z","created_at":"2023-10-20T14:37:26.000000Z","id":1}

Conclusion

In conclusion, mastering the Laravel creating event empowers developers to effortlessly automate processes, ensuring a seamless and enchanting user experience. Elevate your Laravel sorcery by incorporating model events into your arsenal. Unleash the magic, streamline workflows, and let your applications sparkle with efficiency.

Comments (0)

Comment


Note: All Input Fields are required.