Laravel / Livewire:在更新时忽略“image”规则

huangapple go评论58阅读模式
英文:

Laravel / Livewire: Ignore "image" rule when updating

问题

简单来说,我必须更新用户。

这是我用于图像的验证规则:

protected $rules = [
    //其他规则
    "profile_image" => "sometimes|image|max:1024",
];

挂载方法:

public function mount($user)
{
    //其他
    $this->profile_image = $user->profile_image;
} 

验证规则按预期工作,但问题是我将图像存储到数据库中作为字符串:示例如下 profile_image: "public/images/tprYt36hk0Ztun9yKBYpUGEX3TJuAaBWMi5bSkTZ.jpg"

这意味着当我想要更新用户而不更改配置文件图像时,我无法做到,因为 image 验证规则的存在,我只能在选择图像时才能通过。

这是我的更新方法:

public function updateCook()
{
    $this->validate();
    try {

        $image = $this->profile_image->store("public/images");

        $this->user->update([
            //其他更新
            "profile_image" => $image,
        ]);

        session()->flash('success', '用户已更新');

    } catch (\Exception $e) {
        session()->flash('error', '发生错误');
    }
}

如果我删除 image 验证规则并尝试提交没有上传新图像的表单,我会得到 Call to a member function store() on string

英文:

Simply put, i have to update the user.

This is my validation rule for the image:

protected $rules = [
    //other rules
    "profile_image" => "sometimes|image|max:1024",
];

Mount method:

public function mount($user)
{
    //other
    $this->profile_image = $user->profile_image;
} 

The validation rules work as expected, but the problem is i store the image into the database as a string: example of how it looks profile_image: "public/images/tprYt36hk0Ztun9yKBYpUGEX3TJuAaBWMi5bSkTZ.jpg"

This means that when i want to update the user without changing the profile image, i can't because of the image validation rule, i can only pass when an image is selected.

This is how my update method looks:

public function updateCook()
{
    $this->validate();
    try {

        $image = $this->profile_image->store("public/images");

        $this->user->update([
            //other updates
            "profile_image" => $image,
        ]);

        session()->flash('success', 'User is updated');

    } catch (\Exception $e) {
        session()->flash('error', 'An error occured');
    }
}

If i remove the image validation rule and try submitting the form without uploading a new image i get Call to a member function store() on string.

答案1

得分: 1

我猜你正在将 $this->profile_image 赋值为 $this->user->profile_image。你不应该这样做,因为它们具有不同的类型。

public function mount($user)
{
    //其他内容
    //$this->profile_image = $user->profile_image;
} 

如果你想在视图中显示图像,使用 $this->user->profile_image

对于 $rules

protected $rules = [
    //其他规则
    "profile_image" => "nullable|image|max:1024",
];

updateCook() 方法中:

//...
if ($this->profile_image) {
    $image = $this->profile_image->store("public/images");
    $this->user->update([
       //其他更新
       "profile_image" => $image,
    ]);

   session()->flash('success', '用户已更新');
}
//...
英文:

I guess you are assigning $this->profile_image = $this->user->profile_image. You shouldn't do that because they have different types

public function mount($user)
{
    //other
    //$this->profile_image = $user->profile_image;
} 

If you want show image in view, use $this->user->profile_image instead

For $rules:

protected $rules = [
    //other rules
    "profile_image" => "nullable|image|max:1024",
];

Within updateCook() method:

//...
if ($this->profile_image) {
    $image = $this->profile_image->store("public/images");
    $this->user->update([
       //other updates
       "profile_image" => $image,
    ]);

   session()->flash('success', 'User is updated');
}
//..

.

答案2

得分: 0

我找到了一个答案:

protected $rules = [
    "profile_image" => "nullable|image|max:1024",
];

然后,如@Khan Tran所说,我只是在`mount()`方法中删除了`$this->profile_image = $user->profile_image;`

在我的`updateCook()`方法中,我做了这样的操作:

```php
public function updateCook()
{
    $this->validate();
    try {
        if ($this->profile_image) {
            $image = $this->profile_image->store("public/images");
            $this->user->update([
                // +其他更新
                "profile_image" => $image,
            ]);
        } else {
            // 没有上传新图片,只更新其他字段
            $this->user->update([
                //其他更新
            ]);
        }

        session()->flash('success', '用户已更新');

    } catch (\Exception $e) {
        session()->flash('error', '更新用户时出错。');
    }
}
英文:

I have found an answer:

protected $rules = [
    "profile_image" => "nullable|image|max:1024",
];

Then as @Khan Tran said i simply removed $this->profile_image = $user->profile_image; in the mount() method

In my updateCook() method i did this:

public function updateCook()
{
    $this->validate();
    try {
        if ($this->profile_image) {
            $image = $this->profile_image->store("public/images");
            $this->user->update([
                // + other updates
                "profile_image" => $image,
                
            ]);
        } else {
            // No new image is uploaded, update other fields only
            $this->user->update([
                //Oter updates
            ]);
        }

        session()->flash('success', 'User is updated');      

    } catch (\Exception $e) {
        session()->flash('error', 'Грешка при ажурирањето на корисникот.');
    }
}

huangapple
  • 本文由 发表于 2023年4月17日 16:10:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定