Laravel - Eloquent更新不使用mutators

时间:2013-05-29 12:04:30

标签: php laravel eloquent

我有一个事件模型,我可以更新事件的开始日期。我有一个改变者:

protected function setStartAttribute($value)
{
    $date = \Datetime::createFromFormat('d.m.Y H:i:s', $value);
    $this->attributes['start'] = $date->format('Y-m-d H:i:s');
}

我需要更新具有相同代码的事件的集合,并且我调用那些更新,并且我没有获得有效日期,它在数据库中输入0000-00-00 00:00:00,但验证通过。< / p>

public function putDate($code)
{
    $models = Events::where('code','=',$code);
    $rules = array(
        'start' => 'required|date_format:d.m.Y H:i:s'
        );
    $validate = Validator::make(Input::all(), $rules);

    if ($validate->fails())
    {
        return Redirect::action('AdminEventsController@getShow', $code)
                    ->withErrors($validate->messages())
                    ->withInput();
    }

    $models->update(array('start' => Input::get('start')));

    setMsg('Saved!');
    return Redirect::action('AdminEventsController@getShow', $code);
}

我找到了这样一个有效的解决方案:

public function putDate($code)
{
    $models = Events::where('code','=',$code)->get();
    $rules = array(
        'start' => 'required|date_format:d.m.Y H:i:s'
        );
    $validate = Validator::make(Input::all(), $rules);

    if ($validate->fails())
    {
        return Redirect::action('AdminEventsController@getShow', $code)
                    ->withErrors($validate->messages())
                    ->withInput();
    }

    foreach ($models as $model)
    {
        $model->start = Input::get('start');
        $model->save();
    }

    setMsg('Saved!');
    return Redirect::action('AdminEventsController@getShow', $code);
}

这里有什么问题?更新方法是否跳过mutator?

0 个答案:

没有答案