【问题标题】:Laravel 5 ON creating process on a model i get errorLaravel 5 ON 在模型上创建过程我得到错误
【发布时间】:2015-07-21 15:28:10
【问题描述】:

在我的控制器函数中想要创建一个模型。

class PropertyController extends AdminController {


  public function langUpdate(Request $request)
  {
      $result = PropertyRltLang::create($request->all());
      return back()->with('resultLangUpdate',$result);
  }
}

PropertyRltLang 是一个模型,它扩展了我自己的模型类(它的名字是 MyBaseModel)。有我的扩展模型类:

class MyBaseModel extends Model{

protected static $onInsertRules = [];

protected static $onUpdateRules = [];

public function __construct()
{
    parent::boot();

    static::creating(function($model)
    {
        $input = Input::all();
        $validate = Validator::make($input, $model::$onInsertRules);
        if($validate->fails()){
            return back()->withErrors($validate->errors());
        }
    });
}
}

如果输入值为真,我会收到此错误:

2/2 Connection.php 第 624 行中的 QueryException: SQLSTATE[23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(educate.property_rlt_lang,CONSTRAINT property_rlt_lang_prop_id_foreign FOREIGN KEY (prop_id) REFERENCES properties (@987654328 @)) (SQL: 插入property_rlt_lang() 值())

但是,如果从具有相同值的模型(Laravel 5 框架类)请求扩展的 PropertyRltLang 模型类可以成功运行。为什么?我如何处理“创建”或“保存”事件?因为创建这个 MyBaseModel 类的原因是当我发送带有错误值的请求时出错。如何处理?

【问题讨论】:

  • 你向控制器发送什么,为什么要验证所有输入?
  • 我已经编辑了我的问题错误这是正确的查询异常错误。
  • 发送到控制器函数的输入是:array (size=4) '_token' => string 'JCn95mnf5NDjw0hOD5frf92O9SvWplaWVKrywsID' (length=40) 'prop_id' => string '2' (length=1) ' prop_text' => 字符串'efsdfd'(长度=6)'lang_name' => 字符串'fr'(长度=2)

标签: php redirect laravel laravel-5 laravel-validation


【解决方案1】:

我放弃了这种方法。现在从请求提供者执行此控制。谢谢你的有趣..

【讨论】:

    【解决方案2】:

    Connection.php 第 624 行中的 2/2 QueryException:SQLSTATE[23000]:完整性约束违规:1452 无法添加或更新子行:外键约束失败(educate.property_rlt_lang,CONSTRAINT property_rlt_lang_prop_id_foreign FOREIGN KEY (prop_id) REFERENCES properties(id)) (SQL: insert into property_rlt_lang() values())

    此错误表明您正在尝试填写外部表中没有任何条目的详细信息。

    另外,如果您使用 create(),请定义表格的可填充列,例如:

    protected $fillable = ['name', 'email', 'password'];
    

    【讨论】:

      【解决方案3】:

      如果您只是想获得一种有效的语言,那么:

      protected static $onInsertRules = array('lang' => 'require');
      $input = array('lang' => Input::get('lang_name');
      

      您需要查看验证文档以了解有关如何正确验证内容的更多信息。 Here

      我仍然不能 100% 确定您要验证的内容。如果此 dosnt 修复,您能否更新您的问题,提供有关您尝试做什么的更多信息?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-16
        • 2017-09-02
        • 2014-03-07
        • 2016-04-15
        • 2016-09-08
        • 1970-01-01
        • 2016-04-02
        • 1970-01-01
        相关资源
        最近更新 更多