<commit_before><?php
namespace Baum\Extensions;

trait ModelExtensions {

  /**
   * Reloads the model from the database.
   *
   * @return \Baum\Node
   */
  public function reload() {
    if ( !$this->exists )
      return $this;

    $dirty = $this->getDirty();
    if ( count($dirty) === 0 )
      return $this;

    $fresh = static::find($this->getKey());

    $this->setRawAttributes($fresh->getAttributes(), true);

    return $this;
  }

  /**
   * Find first model.
   *
   * @return \Illuminate\Database\Eloquent\Model
   */
  public static function first() {
    $instance = new static;

    return $instance->newQuery()->orderBy($instance->getKeyName(), 'asc')->first();
  }

  /**
   * Find last model.
   *
   * @return \Illuminate\Database\Eloquent\Model<commit_msg>Modify Model::reload() function behaviour to sync with original attributes when model is not persisted and to always load from the database otherwise.<commit_after><?php
namespace Baum\Extensions;

trait ModelExtensions {

  /**
   * Reloads the model from the database.
   *
   * @return \Baum\Node
   */
  public function reload() {
    if ( !$this->exists ) {
      $this->syncOriginal();
    } else {
      $fresh = static::find($this->getKey());

      $this->setRawAttributes($fresh->getAttributes(), true);
    }

    return $this;
  }

  /**
   * Find first model.
   *
   * @return \Illuminate\Database\Eloquent\Model
   */
  public static function first() {
    $instance = new static;

    return $instance->newQuery()->orderBy($instance->getKeyName(), 'asc')->first();
  }

  /**
   * Find last model.
   *
   * @return \Illuminate\Database\Eloquent\Model
{'fraction_non_alphanumeric': 0.15594660194174756, 'fraction_numerical': 0.0006067961165048543, 'mean_word_length': 2.8708920187793425, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5381788', 'n_tokens_mistral': 522, 'n_tokens_neox': 513, 'n_words': 119}