2015-02-09 21 views
5

Tôi đang tạo yếu tố biểu mẫu đa lựa chọn để cập nhật trường học và đặc sản pivot table school_specialty. Vấn đề là khi tôi thay đổi chỉ một cái gì đó trong đa lựa chọn không đầu vào khác hoặc textareas, tôi không thể nghe các sự kiện mô hình vì vậy tôi không thể đồng bộ bảng school_specialty. Nhưng khi tôi điền vào bất kỳ đầu vào khác, nó hoạt động hoàn hảo. Dưới đây là đa chọn của tôi từ lưỡi:Xác thực Laravel với nhiều lựa chọn

{{Form::select('specialties[]', $specialties_data, $school->specialties, array('multiple' => 'true', 'id' => 'multi-select'))}} 

Đây là phương pháp cập nhật của tôi từ bộ điều khiển học:

public function update($id) 
{ 
    $data = Input::only('name', 'type_id', 'description', 'info_specialties', 'contacts', 'specialties', 'financing_id', 'district_id', 'city_id'); 

    $school = School::find($id); 
    $school->name = $data['name']; 
    $school->type_id = $data['type_id']; 
    $school->description = $data['description']; 
    $school->info_specialties = $data['info_specialties']; 
    $school->contacts = $data['contacts']; 
    $school->cover_photo = Input::file('cover_photo'); 
    $school->set_specialties = $data['specialties']; 
    $school->financing_id = $data['financing_id']; 
    $school->set_district_id = $data['district_id']; 
    $school->city_id = $data['city_id']; 

    try { 
     $school->save(); 
    } catch (ValidationException $errors) { 
     return Redirect::route('admin.schools.edit', array($id)) 
      ->withErrors($errors->getErrors()) 
      ->withInput(); 
    } 

    return Redirect::route('admin.schools.edit', array($id)) 
     ->withErrors(array('mainSuccess' => 'School was created.')); 
} 

Và đây là ví dụ mô hình trường học của tôi:

<?php 

class School extends Eloquent { 
    protected $table = 'schools'; 

    protected $fillable = array('name', 'type_id', 'description', 'city'); 
    protected $guarded = array('id'); 
    protected $appends = array('specialties'); 

    public $set_specialties; 
    public $set_district_id; 

    protected static function boot() 
    { 
     parent::boot(); 

     static::updating(function($model) 
     { 
      $data = array(
       'name' => $model->name, 
       'type_id' => $model->type_id, 
       'description' => $model->description, 
       'specialties' => $model->set_specialties, 
       'city_id' => $model->city_id 
      ); 

      $rules = array(
       'name' => 'required|min:3|max:50', 
       'type_id' => 'required|min:1|max:300000', 
       'description' => 'required|min:10', 
       'specialties' => 'required|array', 
       'city_id' => 'required|min:1|max:300000' 
      ); 

      $validator = Validator::make($data, $rules); 

      if ($validator->fails()) { 
       throw new ValidationException(null, null, null, $validator->messages()); 
      } else { 
       return true; 
      } 
     }); 

     static::updated(function($model) 
     { 
      if ($model->set_specialties != null) 
      { 
       $model->specialty()->sync($model->set_specialties); 
      } 
     }); 
    } 

    public function specialty() 
    { 
     return $this->belongsToMany('Specialty', 'school_specialty'); 
    } 
} 
?> 

Trả lời

0

Khi cập nhật chỉ đặc trường học Các sự kiện mô hình trường học không được kích hoạt vì mô hình Trường vẫn giữ nguyên.

Tôi nghĩ giải pháp đơn giản và thanh lịch nhất là chạm vào trường hợp mô hình trường học. Điều này sẽ sửa đổi trường updated_at cho đối tượng School và do đó kích hoạt các sự kiện mô hình. Để thực hiện việc này, hãy thêm các dòng sau trước khối try/catch:

if ($school->set_specialties !== null) { 
    $school->touch(); 
} 

Ngoài ra, xác thực không được xử lý trong người quan sát mô hình. Kiểm tra xác thực yêu cầu biểu mẫu tại đây: https://laravel.com/docs/5.6/validation#form-request-validation.

Các vấn đề liên quan