【问题标题】:Laravel 5 Add custom attribute in paginate()Laravel 5 在 paginate() 中添加自定义属性
【发布时间】:2018-07-12 03:30:45
【问题描述】:

我想在模型中添加自定义属性。我说:

// app/models/xxxModel.php
public function getTituloAttribute()
{
    return 'Cotizacion número: ' . $this->attributes['number'];
}

然后在Controller中:

    $quotations         = AdminQuotations::orderBy('id', 'desc')->paginate()->toArray();

我想在视图中使用 xxxmodel->titulo,但不出现这个新属性。

非常感谢,对不起我的英语。

【问题讨论】:

    标签: laravel-5 pagination models


    【解决方案1】:

    您可以添加一个名为 appends 的受保护属性来附加不是来自数据库的属性列表。

    class AdminQuotations
    {
        protected $appends = [
            'titulo'
        ];
    }
    

    【讨论】:

      【解决方案2】:

      不,对不起, 只有我删除了:->toArray()

      仅此而已,添加自定义属性的代码很好

      谢谢!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-20
        • 2016-01-01
        • 2012-07-10
        • 1970-01-01
        • 2017-07-29
        • 2020-03-22
        • 2013-06-18
        • 2020-07-02
        相关资源
        最近更新 更多