无涯教程: Laravel 8 - 模型事件
2021/7/10 13:05:46
本文主要是介绍无涯教程: Laravel 8 - 模型事件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
在这里,我将向您展示laravel 8模型事件。Laravel提供eloquent模型事件列表,每个模型事件都有自己的函数。
creating - 插入前调用。
creating - 插入后调用。
updating - 更新前调用。
updated - 更新后调用。
deleting - 删除前调用。
deleted - 删除后调用。
retriveved - 从数据库查询数据时调用。
saving - 创建或更新前调用。
saved - 创建或更新后调用。
restoring - 在恢复前调用。
restored - 恢复后调用。
replicating - 调用重复数据。
创建Product Model
在这里,我们将使用事件创建Product模型。所以让我们创建并写入以下代码:
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); }); } }
创建记录:Creating和Created事件
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'); } }
输出日志文件:
[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}
更新记录:Updating和Updated事件
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'); } }
输出日志文件:
[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"}
删除记录:Deleted事件
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'); } }
输出日志文件:
[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"}
这篇关于无涯教程: Laravel 8 - 模型事件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-22项目:远程温湿度检测系统
- 2024-12-21《鸿蒙HarmonyOS应用开发从入门到精通(第2版)》简介
- 2024-12-21后台管理系统开发教程:新手入门全指南
- 2024-12-21后台开发教程:新手入门及实战指南
- 2024-12-21后台综合解决方案教程:新手入门指南
- 2024-12-21接口模块封装教程:新手必备指南
- 2024-12-21请求动作封装教程:新手必看指南
- 2024-12-21RBAC的权限教程:从入门到实践
- 2024-12-21登录鉴权实战:新手入门教程
- 2024-12-21动态权限实战入门指南