【问题标题】:Laravel Eloquent to treat the column 'archived_at' like it does 'deleted_at'Laravel Eloquent 像对待 'deleted_at' 一样对待列 'archived_at'
【发布时间】:2014-08-15 22:40:57
【问题描述】:

我试图让 Laravel 以与处理“deleted_at”相同的方式处理新添加的名为“archived_at”的列。也就是说,当我运行查询时,我不希望它获取任何存档记录,除非我明确告诉它(即 Invoices::onlyArchived()->paginate())。换句话说,archived_at 行为应该模仿 Laravel 内置的软删除行为。

我看到 Laravel 的 Builder 和 Model 类中内置了软删除功能。到目前为止,这是我所拥有的:

  1. 扩展 Model 类并在 new 中添加 archived_at 功能 类。
  2. 更新app/config.php别名如下:
    // 'Eloquent'        => 'Illuminate\Database\Eloquent\Model',
    'Eloquent'        => 'CoreExtensions\Model',

我似乎无法弄清楚如何扩展 Builder 类并强制 Laravel 从扩展的 Builder 类中读取。如果我能做到这一点,我可能会弄清楚其余的。有什么建议么?提前致谢!

【问题讨论】:

    标签: php class laravel-4


    【解决方案1】:

    您可能只需要扩展 Eloquent 并执行以下操作:

    <?php
    
    class BaseModel extends Eloquent {
    
        public static function withArchived()
        {
            return with(new static)->newQuery(true, true);
        }
    
        public static function onlyArchived()
        {
            return with(new static)->newQuery(false, false, true);
        }
    
        /**
         * Get a new query builder for the model's table.
         *
         * @param  bool  $excludeDeleted
         * @return \Illuminate\Database\Eloquent\Builder|static
         */
        public function newQuery($excludeDeleted = true, $excludeArchived = true, $onlyArchived = false)
        {
            $builder = $this->newEloquentBuilder($this->newBaseQueryBuilder());
    
            // Once we have the query builders, we will set the model instances so the
            // builder can easily access any information it may need from the model
            // while it is constructing and executing various queries against it.
            $builder->setModel($this)->with($this->with);
    
            if ( ! $onlyArchived)
            {
                if ($excludeDeleted && $this->softDelete)
                {
                    $builder->whereNull($this->getQualifiedDeletedAtColumn());
                }
    
                if ($excludeArchived)
                {
                    $builder->whereNull('archived_at');
                }
            }
            else
            {
                $builder->whereNotNull('archived_at');
            }
    
            return $builder;
        }
    
    }
    

    然后你可能能够:

    Invoices::onlyArchived()->paginate();
    
    Invoices::withArchived()->where('customer_id', $customer_id)->paginate();
    

    【讨论】:

      【解决方案2】:

      如果你运行的是 Laravel 4.2,你可以查看这两个文件,我已经基于内置的 SoftDelete trait 创建了我自己的 trait。 (这比上面的答案灵活得多。)

      • Illuminate/Database/Eloquent/SoftDeletingScope.php
      • Illuminate/Database/Eloquent/SoftDeletingTrait.php

      http://laravel.io/forum/08-14-2014-add-filter-to-builder-on-a-custom-method-call

      【讨论】:

      • 您将自定义文件放在哪里?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-12
      • 1970-01-01
      • 1970-01-01
      • 2021-01-10
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      相关资源
      最近更新 更多