In my controller function want to create a model.
class PropertyController extends AdminController {
public function langUpdate(Request $request)
{
$result = PropertyRltLang::create($request->all());
return back()->with('resultLangUpdate',$result);
}
}
PropertyRltLang is a model and it's extend my own model class(it's name is MyBaseModel). There is my extended model class:
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());
}
});
}
}
With true input values i get this error:
2/2 QueryException in Connection.php line 624: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
educate
.property_rlt_lang
, CONSTRAINTproperty_rlt_lang_prop_id_foreign
FOREIGN KEY (prop_id
) REFERENCESproperties
(id
)) (SQL: insert intoproperty_rlt_lang
() values ())
But PropertyRltLang model class if extended from Model (Laravel 5 framework class) request with same values are working successfully. Why? And i how to handle "creating" or "save" events? Because reason of create this MyBaseModel class is getting errors when i send a request with wrong values. How to handle this?