如果条件失败,Laravel会从模型抛出错误 [英] Laravel throw Error from Model if the Condition Fails

查看:281
本文介绍了如果条件失败,Laravel会从模型抛出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对模型进行所有验证

我的规则是

public static $rules = array(
        'VehicleNumber' => 'required|unique:vehicle', 
        'NumberSeats' => 'required', 
        'VehicleType' => 'required', 
        'MaximumAllowed' => 'numeric|max:3',
        'VehicleCode' => 'required|unique:vehicle');
}

但是为了更改文件名并检查其大小,我在SetFooAttribute

中进行处理

public function setInsurancePhotoAttribute($InsurancePhoto)
{
    if($InsurancePhoto && Input::file('InsurancePhoto')->getSize() < '1000')
    {
    $this->attributes['InsurancePhoto'] = Input::get('VehicleCode') . '-InsurancePhoto.' . Input::file('InsurancePhoto')->getClientOriginalExtension();
    Input::file('InsurancePhoto')->move('assets/uploads/vehicle/', Input::get('VehicleCode') . '-InsurancePhoto.' . Input::file('InsurancePhoto')->getClientOriginalExtension());
    }
}

有条件

if($InsurancePhoto && Input::file('InsurancePhoto')->getSize() < '1000')

如果图像存储失败,则不会完成设置属性.

但是在这种情况下,我如何向用户抛出错误?

解决方案

getSize()返回整数,因此您不应将字符串用于比较,因为您已经在单引号中写了1000.您可以在laravel中创建自己的自定义验证.检查此链接 stackoverflow 解决方案

getSize() return integer so you should not use string for comparison as you have written 1000 in single quotes. You can create your own custom Validation in laravel. Check this links stackoverflow and http://culttt.com/2014/01/20/extending-laravel-4-validator/

这篇关于如果条件失败,Laravel会从模型抛出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆