예제 #1
0
 /**
  * Update the specified resource in storage
  *
  * @api
  * @param  \App\Http\Request  $request
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function update(Request $request, $id)
 {
     $this->item = call_user_func($this->model . '::findOrFail', $id);
     $this->beforeSave();
     $this->beforeUpdate();
     $this->validate($request, $this->rules, $this->messages);
     $data = array_merge(request()->all(), $this->data);
     $this->item->update($data);
     $this->afterSave();
     $this->afterUpdate();
     if ($request->ajax()) {
         return json_encode(['status' => 'success', 'data' => null]);
     } else {
         return $this->redirector->getRedirect('store')->with('success', $this->name_singular . ' was updated successfully.');
     }
 }
예제 #2
0
 /**
  * Update the model in the database.
  *
  * @param  array  $attributes
  * @return mixed
  */
 public function update(array $attributes = array())
 {
     $instance = new static();
     $fillable = isset($instance->fillable) ? $instance->fillable : array();
     // fallback to default language
     if (!isset($attributes['lang']) or !$attributes['lang']) {
         if (isset(static::$lang) and static::$lang or isset($fillable) and in_array('lang', $fillable)) {
             $attributes['lang'] = ci()->translate->default_language();
         }
     }
     foreach ($attributes as $key => $value) {
         // we have expires_at/starts_at field with value like: 2013-05-15 instead of UNIX timestamps
         // used in datepicker inputs
         if (ends_with($key, '_at') and strpos($value, '-')) {
             switch ($key) {
                 case 'expires_at':
                     $attributes[$key] = strtotime($value . ' 23:59:59');
                     break;
                 case 'starts_at':
                     $attributes[$key] = strtotime($value . ' 00:00:00');
                     break;
                 default:
                     $attributes[$key] = strtotime($value . ' 00:00:00');
                     break;
             }
         } elseif (ends_with($key, '_at')) {
             $timestamp = $attributes[$key];
             if ((int) $timestamp === $timestamp and $timestamp <= PHP_INT_MAX and $timestamp >= ~PHP_INT_MAX) {
                 // already a timestamp
                 $attributes[$key] = $timestamp;
             } else {
                 $format = ci()->settings->date_format;
                 $validated = \DateTime::createFromFormat($format, $timestamp);
                 if ($validated) {
                     $attributes[$key] = $validated->getTimestamp();
                 }
             }
         }
     }
     return parent::update($attributes);
 }