-2

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, CONSTRAINT property_rlt_lang_prop_id_foreign FOREIGN KEY (prop_id) REFERENCES properties (id)) (SQL: insert into property_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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Mesuti
  • 878
  • 1
  • 13
  • 29
  • what are you sending to the controller, why are you validation all Inputs? – Ashley Wrench May 11 '15 at 08:03
  • i have edit my question error this is right query exception error. – Mesuti May 11 '15 at 08:04
  • sent to controller function inputs are: array (size=4) '_token' => string 'JCn95mnf5NDjw0hOD5frf92O9SvWplaWVKrywsID' (length=40) 'prop_id' => string '2' (length=1) 'prop_text' => string 'efsdfd' (length=6) 'lang_name' => string 'fr' (length=2) – Mesuti May 11 '15 at 08:07

3 Answers3

0

if your only trying to get a valid language, then:

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

you need to look at the validation documentation to find more on how to validate stuff properly. Here

I'm still not 100% sure on what you are trying to validate. If this dosnt fix, could you update your question with more info on what you are trying to do?

Ashley Wrench
  • 969
  • 8
  • 25
0

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, CONSTRAINT property_rlt_lang_prop_id_foreign FOREIGN KEY (prop_id) REFERENCES properties (id)) (SQL: insert into property_rlt_lang () values ())

This error suggests that your are trying to fill details which does not have any entry in the foreign table.

Also if you are using create() then please define fillable columns of the table e.g:

protected $fillable = ['name', 'email', 'password'];
Khan Shahrukh
  • 6,109
  • 4
  • 33
  • 43
0

I give up from this method. And now doing this controls from Request provider. Thanks for interesting..

Mesuti
  • 878
  • 1
  • 13
  • 29