【问题标题】:How to detect update event in model in Laravel 8如何在 Laravel 8 中检测模型中的更新事件
【发布时间】:2021-05-12 10:39:21
【问题描述】:

祝大家有美好的一天 情况如下 在控制器中,在更新方法中,我尝试更新对象 此对象的字段中有图像 写了一个特征来处理这个字段并加载一个图像 在模型本身中,我调用了update方法,它只是确定了更新对象的事件 问题出在指定目录下的图片被加载并且数据库中的条目本身没有改变 这是我的代码

  1. 控制器
  2. 型号
  3. 特质

模型中有额外的代码

public function update(Request $request, MainHeader $mainHeader): RedirectResponse
    {
        $mainHeader->update([
            'language_id' => $request->language_id,
            'brandLogoImage' => $request->file('brandLogoImage'),
            'homeTitle' => $request->homeTitle,
            'ourProjectsTitle' => $request->ourProjectsTitle,
            'contactTitle' => $request->contactTitle,
            'feedbackTitle' => $request->feedbackTitle,
        ]);
        return redirect()->route('admin.header.index')->with('success', 'Данные успешно обновлены');
    }
public function setBrandLogoImageAttribute($value): string
    {
        return $this->uploadImage('brandLogoImage', $value);
    }

    public function update(array $attributes = [], array $options = [])
    {
        $this->uploadImage('brandLogoImage', $attributes['brandLogoImage']);
        $this->setBrandLogoImageAttribute($attributes['brandLogoImage']);
        return parent::update($attributes, $options); // TODO: Change the autogenerated stub
    }
protected function uploadImage(string $attr, $value): string
    {
        $uploadDir = public_path('uploads/');
        $imageDir = public_path('uploads/image/');
        if (!file_exists($uploadDir)){
            mkdir($uploadDir);
        }
        if (!file_exists($imageDir)){
            mkdir($imageDir);
        }
        if (!file_exists(public_path("uploads/image/$this->table/"))){
            mkdir(public_path("uploads/image/$this->table/"));
        }
        $imageName = Str::random(12) . '.png';
        Image::make($value)->save(public_path("uploads/image/$this->table/$imageName") , 100);
        return $this->attributes[$attr] = (string) "uploads/image/$this->table/$imageName";
    }

【问题讨论】:

  • 我不明白您要完成什么,为什么要覆盖更新? ,我写了一个解释事件的答案,因为您正在覆盖更新以在默认更新之前执行 smlthng。你能详细说明你的问题吗
  • 我只是想另一种方式注入一些业务逻辑
  • 我不明白为什么你会这样使用 update(),你可以在调用 update 之前在控制器中完成所有操作,如果你愿意,我可以向你展示如何正确收听更新
  • 您的目的是了解如何检测更新吗?或者如何上传文件并将其位置保存为字段?

标签: laravel laravel-5 eloquent


【解决方案1】:

如果您在模型中调用更新方法,那么您将覆盖模型类的默认 update(),它不会监听事件,它只是在 parent:: 之前运行您的代码,因此您需要确保您所做的更改不会被父调用覆盖。

关于您关于如何检测更新的问题,如果您想在更新之前执行任何操作,我建议您使用eloquent events 或使用observers,观察者会听取有关您的模型的各种事件,例如更新或更新.. 但是我认为如果它仅用于更新事件,那么您应该使用event using closure

例如:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * The "booted" method of the model.
     *
     * @return void
     */
    protected static function booted()
    {
        static::updating(function ($user) {
            // do what you want
        });
    }
}

如果你的目的

【讨论】:

  • 看起来不错,我会试试谢谢帮助
  • 非常欢迎您@ЕвгенийТимченко。检查其他方法的链接
猜你喜欢
  • 1970-01-01
  • 2020-09-02
  • 2021-10-12
  • 1970-01-01
  • 2017-05-08
  • 2021-11-20
  • 1970-01-01
  • 1970-01-01
  • 2016-08-28
相关资源
最近更新 更多