2015-02-09 6 views
5

के साथ लैरवेल प्रमाणीकरण मैं स्कूलों और विशिष्टताओं पिवोट टेबल school_specialty को अपडेट करने के लिए एक बहु चुनिंदा फॉर्म तत्व बना रहा हूं। समस्या यह है कि जब मैं मल्टी सिलेक्ट में कुछ इनपुट या टेक्स्टरेज़ में कुछ बदलता हूं, तो मैं मॉडल इवेंट्स नहीं सुन सकता, इसलिए मैं school_specialty टेबल को सिंक नहीं कर सकता। लेकिन जब मैं कोई अन्य इनपुट भरता हूं तो यह सही काम करता है।मल्टी सिलेक्ट

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.')); 
} 

यहाँ और मेरे उदाहरण स्कूल मॉडल है:

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

इस स्कूल नियंत्रक से मेरे अद्यतन विधि है:

<?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'); 
    } 
} 
?> 

उत्तर

0

केवल स्कूल विशिष्टताओं को अपडेट करते समय यहाँ ब्लेड से मेरी बहु चयन है स्कूल मॉडल की घटनाओं को ट्रिगर नहीं किया जाता है क्योंकि स्कूल मॉडल वही रहता है।

मुझे लगता है कि सबसे सरल और सबसे सुरुचिपूर्ण समाधान स्कूल मॉडल उदाहरण को छूना है। यह स्कूल ऑब्जेक्ट के लिए update_at फ़ील्ड को संशोधित करेगा और इस प्रकार मॉडल ईवेंट ट्रिगर करेगा। इस आज़माएं/कैच ब्लॉक से पहले निम्न पंक्तियां जोड़ें करने के लिए:

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

इसके अलावा, सत्यापन मॉडल पर्यवेक्षकों में से संभाला नहीं किया जाना चाहिए। फॉर्म अनुरोध सत्यापन यहां जांचें: https://laravel.com/docs/5.6/validation#form-request-validation

संबंधित मुद्दे