ItSolutionStuff.com

Laravel Model Creating Event Example

By Hardik Savani • November 5, 2023
Laravel

Hello Artisan,

In this example, I will show you laravel model creating event. In this article, we will implement a how to use model creating event in laravel. I’m going to show you about how to use creating event in laravel model. you can see laravel creating event model.

In Laravel, you can use model events to perform actions when a model is being created, updated, deleted, etc. To demonstrate how to use the `creating` event in a Laravel model, I'll provide you with an example. Let's assume you have a `Post` model, and you want to automatically set a slug for a post before it is created. You can do this using the `creating` event.

I will give you simple example of how to call creating event in from model. so, let's see the very simple example:

Create Post Model:

1. Create the `Post` model using the Artisan command:

php artisan make:model Post

2. In the generated `Post` model, you can define the `creating` event using the `static::creating` method. Here's an example:

app/Models/Post.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Support\Str;

class Post extends Model

{

protected $fillable = ['title', 'content', 'slug'];

/**

* Write code on Method

*

* @return response()

*/

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

});

}

}

Create Post Record:

In this example, when a new `Post` model is being created, the `creating` event is triggered, and we use a closure to generate a slug based on the `title` attribute. We're using the `Str::slug` method from Laravel to create a URL-friendly slug from the title.

3. Now, when you create a new `Post` instance and save it, the `creating` event will automatically set the slug for you. Here's how you can create a new post:

app/Http/Controllers/PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index(Request $request)

{

$post = Post::create([

'title' => 'This is Testing',

'content' => 'This is a Testing'

]);

dd($post);

}

}

Now, you can run following controller code and you will find following log for calling creating event and created event as like the below:

Output:

[2023-10-20 14:37:26] local.INFO: Creating event call: {"title":"This is Testing","content":"This is a Testing"}

[2023-10-20 14:37:26] local.INFO: Created event call: {"title":"This is Testing","content":"This is a Testing","slug":"this-is-

testing","updated_at":"2023-10-20T14:37:26.000000Z","created_at":"2023-10-20T14:37:26.000000Z","id":1}

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

šŸ“ŗ Subscribe on YouTube

We Are Recommending You

ā˜…

Laravel Eloquent Always Load Model Relation Example

Read Now →
ā˜…

Laravel 10 Enum Model Attribute Casting Example

Read Now →
ā˜…

Laravel 10 Model Events Example Tutorial

Read Now →
ā˜…

How to Add Custom Attribute in Laravel Model?

Read Now →
ā˜…

Laravel Model Disable Primary Key & Auto Increment Example

Read Now →
ā˜…

How to Get Columns Names from Model in Laravel?

Read Now →
ā˜…

How to Create Model in Laravel using Command?

Read Now →
ā˜…

Laravel 9 Enum Model Attribute Casting Example

Read Now →
ā˜…

How to use Model Events in Laravel 8?

Read Now →
ā˜…

Laravel Model Caching - Performance Boost Tutorial

Read Now →
ā˜…

Laravel Improve Speed Performance using Model Caching Tutorial

Read Now →
ā˜…

Laravel Model Disable created_at and updated_at Update Record

Read Now →
ā˜…

How to disable model timestamps in Laravel?

Read Now →
ā˜…

How to Get Table Name from Model in Laravel?

Read Now →