Ejemplo n.º 1
0
 protected static function boot()
 {
     parent::boot();
     static::creating(function ($model) {
         $data = array('name' => $model->name);
         $rules = array('name' => 'required|min:3|max:50');
         $validator = Validator::make($data, $rules);
         if ($validator->fails()) {
             throw new ValidationException(null, null, null, $validator->messages());
         } else {
             return $model->validate();
         }
     });
     static::updating(function ($model) {
         $data = array('name' => $model->name);
         $rules = array('name' => 'required|min:3|max:50');
         $validator = Validator::make($data, $rules);
         if ($validator->fails()) {
             throw new ValidationException(null, null, null, $validator->messages());
         } else {
             return true;
         }
     });
     static::deleting(function ($model) {
         $municipalities = Municipality::where('district_id', '=', $model->id)->get();
         foreach ($municipalities as $municipality) {
             $municipality = Municipality::find($municipality->id)->delete();
         }
         return true;
     });
 }
Ejemplo n.º 2
0
 public function validate()
 {
     if (is_null($this::where('name', '=', $this->name)->first())) {
         if (!is_null(Municipality::where('id', '=', $this->municipality_id)->first())) {
             return true;
         } else {
             throw new ValidationException(null, null, null, array('municipality_id' => "Грешен ID на общината."));
         }
     } else {
         throw new ValidationException(null, null, null, array('name' => "Градът/селото вече съществува."));
     }
 }