How to use Model Events in Laravel 8?

About Us:

We are from free coder. We are a group of web developers who are passionate about web development. You can learn from us php, laravel, node js, vue js, react js and many other topic on web developement.
we are very happy to help you.
free coder

Today what you are going to learn:

Hello Brother, i will show you how to works laravel 8 model events lesson. you can see model events in laravel 8. i would like to share with you eloquent model events laravel 8. I’m going to show you about laravel 8 model events created. follow bellow step for laravel 8 model events updated.

Laravel provide list of eloquent model events and each model event have it's own function. it's very helpful. i love laravel eloquent model events.

  • creating: Call Before Create Record.

  • created: Call After Created Record.

  • updating: Call Before Update Record.

  • updated: Class After Updated Record.

  • deleting: Call Before Delete Record.

  • deleted: Call After Deleted Record.

  • retrieved: Call Retrieve Data from Database.

  • saving: Call Before Creating or Updating Record.

  • saved: Call After Created or Updated Record.

  • restoring: Call Before Restore Record.

  • restored: Call After Restore Record.

  • replicating: Call on replicate Record.

Sometime, we need to add unique number or create slug from title then before create record you need to update this fields at time we can use those time of event. same thing when you update records then also you have to update slug and might when remove records then also you have to delete child records. so event is very helpful and i will say you must have to use when you need this type of situation.

I will explain few important events with lesson so you will understand how it works and how you can use it.

In this lesson i will write down creating, created, updating, updated and deleted events and i will show you one by one lesson with output in log file so you can easily understand, how model events works.

Create Product Model with events

Hello Brother, we will create product model with events. so let's create and put bellow code:

app/Models/Product.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;

use Illuminate\Database\Eloquent\Model;

use Log;

use Str;

class Product extends Model

{

use HasFactory;

protected $fillable = [

'name', 'slug', 'detail'

];

/**

* Write code on Method

*

* @return response()

*/

public static function boot() {

parent::boot();

/**

* Write code on Method

*

* @return response()

*/

static::creating(function($item) {

Log::info('Creating event call: '.$item);

$item->slug = Str::slug($item->name);

});

/**

* Write code on Method

*

* @return response()

*/

static::created(function($item) {

/*

Write Logic Here

*/

Log::info('Created event call: '.$item);

});

/**

* Write code on Method

*

* @return response()

*/

static::updating(function($item) {

Log::info('Updating event call: '.$item);

$item->slug = Str::slug($item->name);

});

/**

* Write code on Method

*

* @return response()

*/

static::updated(function($item) {

/*

Write Logic Here

*/

Log::info('Updated event call: '.$item);

});

/**

* Write code on Method

*

* @return response()

*/

static::deleted(function($item) {

Log::info('Deleted event call: '.$item);

});

}

}

Now here, we will simply call one by one model method and let's see output:

Create Record: Creating and Created Event

app/Http/Controllers/ProductController.php

<?php

namespace App\Http\Controllers;

use App\Models\Product;

use Illuminate\Http\Request;

class ProductController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

Product::create([

'name' => 'silver',

'detail' => 'This is silver'

]);

dd('done');

}

}

Output Log File:

[2020-10-20 14:37:26] local.INFO: Creating event call: {"name":"silver","detail":"This is silver"}

[2020-10-20 14:37:26] local.INFO: Created event call: {"name":"silver","detail":"This is silver","slug":"silver","updated_at":"2020-10-20T14:37:26.000000Z","created_at":"2020-10-20T14:37:26.000000Z","id":5}

Update Record: Updating and Updated Event

app/Http/Controllers/ProductController.php

<?php

namespace App\Http\Controllers;

use App\Models\Product;

use Illuminate\Http\Request;

class ProductController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

Product::find(5)->update([

'name' => 'silver updated',

'detail' => 'This is silver'

]);

dd('done');

}

}

Output Log File:

[2020-10-20 14:39:04] local.INFO: Updating event call: {"id":5,"name":"silver updated","detail":"This is silver","created_at":"2020-10-20T14:37:26.000000Z","updated_at":"2020-10-20T14:37:26.000000Z","slug":"silver"}

[2020-10-20 14:39:04] local.INFO: Updated event call: {"id":5,"name":"silver updated","detail":"This is silver","created_at":"2020-10-20T14:37:26.000000Z","updated_at":"2020-10-20T14:39:04.000000Z","slug":"silver-updated"}

Delete Record: Deleted Event

app/Http/Controllers/ProductController.php

<?php

namespace App\Http\Controllers;

use App\Models\Product;

use Illuminate\Http\Request;

class ProductController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

Product::find(5)->delete();

dd('done');

}

}

Output Log File:

[2020-10-21 03:14:45] local.INFO: Deleted event call: {"id":5,"name":"silver updated","detail":"This is silver","created_at":"2020-10-20T14:37:26.000000Z","updated_at":"2020-10-20T14:39:04.000000Z","slug":"silver-updated"}

Now you can check from your end.

thank you very much for following up with me.

Recent Posts

Related Posts