5

学校と専門分野のピボット テーブルを更新するための複数選択フォーム要素を作成していますschool_specialty。問題は、他の入力やテキストエリアではなく、複数選択で何かだけを変更すると、モデルイベントをリッスンできないため、school_specialtyテーブルを同期できないことです。しかし、他の入力を入力すると、完璧に機能します。ブレードからの私の複数選択は次のとおりです。

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

これは私の更新方法ですschool controller

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

そして、これが私の例school modelです:

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

1 に答える 1

0

学校の専門分野のみを更新する場合、School モデルは同じままであるため、School モデル イベントはトリガーされません。

最もシンプルでエレガントな解決策は、学校モデルのインスタンスに触れることだと思います。これにより、School オブジェクトの updated_at フィールドが変更され、モデル イベントがトリガーされます。これを行うには、try/catch ブロックの前に次の行を追加します。

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

また、検証はモデル オブザーバーで処理するべきではありません。ここでフォームリクエストの検証を確認してください: https://laravel.com/docs/5.6/validation#form-request-validation .

于 2018-03-11T04:00:24.980 に答える