第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

無(wú)涯教程: Laravel 8 - 模型事件

標(biāo)簽:
laravel

在这里,我将向您展示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"}



點(diǎn)擊查看更多內(nèi)容
TA 點(diǎn)贊

若覺(jué)得本文不錯(cuò),就分享一下吧!

評(píng)論

作者其他優(yōu)質(zhì)文章

正在加載中
  • 推薦
  • 評(píng)論
  • 收藏
  • 共同學(xué)習(xí),寫(xiě)下你的評(píng)論
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說(shuō)多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開(kāi)微信掃一掃,即可進(jìn)行掃碼打賞哦
今天注冊(cè)有機(jī)會(huì)得

100積分直接送

付費(fèi)專欄免費(fèi)學(xué)

大額優(yōu)惠券免費(fèi)領(lǐng)

立即參與 放棄機(jī)會(huì)
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)

舉報(bào)

0/150
提交
取消