【问题标题】:Laravel Eloquent Accessing OriginalLaravel Eloquent 访问原文
【发布时间】:2013-11-20 00:09:25
【问题描述】:

我试图从我从我的一个模型中获得的对象中获取值。它只返回我不想要的属性,因为它与我的表中的内容不对应。 我想访问原始数组。

我做到了:

$entries = Model::where('A', $A)->where('B', $B)->get();

@Foreach ($entries as $entry) 

$entry->id
$entry->name

@Endforeach

我尝试添加->original,但它要么不起作用。

这是我的var_dump($entries)的部分第一个条目

(
    [items:protected] => Array
        (
            [0] => App\Models\TableA Object
                (
                    [table:protected] => Table A
                    [primaryKey] => id
                    [connection:protected] => 
                    [perPage:protected] => 15
                    [incrementing] => 1
                    [timestamps] => 1
                    [attributes:protected] => Array
                        (
                            [id] => 1
                            [name] => 2

                        )

                    [original:protected] => Array
                        (
                            [id] => 1
                            [name] => 1

                        )

【问题讨论】:

  • 你能举例说明你真正想要得到什么吗?我有点困惑。如果您尝试将模型转换为数组,可以使用toArray() 方法。 laravel.com/api/…

标签: orm laravel laravel-4 eloquent


【解决方案1】:

在检索 Eloquent 模型属性的原始值时,您 可以使用getOriginal($key)

参考:

【讨论】:

【解决方案2】:

对于 laravel 4.2 及更高版本

$entries->toArray()

将仅提供模型属性。

【讨论】:

    猜你喜欢
    • 2015-11-15
    • 2020-07-03
    • 2017-02-17
    • 2022-12-06
    • 2019-06-05
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多