Guest User

Untitled

a guest
Apr 20th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. $m = new ModelName;
  2.  
  3. $m->name = 'Test model insert';
  4. $m->save();
  5.  
  6. $m1 = ModelName::find( $m->id );
  7.  
  8. if ($m1) {
  9. $m1->name = 'Test model update';
  10. $m1->save(); // Error occured here.
  11. }
  12.  
  13. trait WithoutUpdateId
  14. {
  15. /**
  16. * Perform a model update operation.
  17. *
  18. * @param IlluminateDatabaseEloquentBuilder $query
  19. *
  20. * @return bool
  21. */
  22. protected function performUpdate(Builder $query)
  23. {
  24. // If the updating event returns false, we will cancel the update operation so
  25. // developers can hook Validation systems into their models and cancel this
  26. // operation if the model does not pass validation. Otherwise, we update.
  27. if ($this->fireModelEvent('updating') === false) {
  28. return false;
  29. }
  30.  
  31. // First we need to create a fresh query instance and touch the creation and
  32. // update timestamp on the model which are maintained by us for developer
  33. // convenience. Then we will just continue saving the model instances.
  34. if ($this->usesTimestamps()) {
  35. $this->updateTimestamps();
  36. }
  37.  
  38. // Once we have run the update operation, we will fire the "updated" event for
  39. // this model instance. This will allow developers to hook into these after
  40. // models are updated, giving them a chance to do any special processing.
  41. $dirty = $this->getDirty();
  42.  
  43. if (count($dirty) > 0) {
  44. unset($dirty['id']);
  45. $this->setKeysForSaveQuery($query)->update($dirty);
  46.  
  47. $this->fireModelEvent('updated', false);
  48.  
  49. $this->syncChanges();
  50. }
  51.  
  52. return true;
  53. }
  54.  
  55. class ModelName extends Model
  56. {
  57. use WithoutUpdateId;
  58. ...
  59. }
Add Comment
Please, Sign In to add comment