laravel模型提供了一种方法,可以从另一个关联表返回结果,这很方便.

例如,我有一个名为item的表和另一个名为feedback的表,其中feedback表存储item表中某个项目的反馈.因此,为了获得id为1的项目的所有反馈,我将执行以下操作:

Item::find(1)->feedback;

接下来是这个对象的打印输出.

Illuminate\Database\Eloquent\Collection Object
(    [items:protected] => Array
       (
           [0] => Feedback Object
               (
                   [table:protected] => feedback
                   [connection:protected] => 
                   [primaryKey:protected] => id
                   [perPage:protected] => 15
                   [incrementing] => 1
                   [timestamps] => 1
                   [attributes:protected] => Array
                       (
                           [id] => 1
                           [rma_id] => 3
                           [item_id] => 8
                           [quo_id] => 0
                           [case_id] => i2eM20160120
                           [line_no] => 000001
                           [content] => test
                           [status] => sent
                           [read] => 0
                           [sender] => Tester
                           [created_at] => 2016-01-20 18:03:44
                           [updated_at] => 2016-01-20 18:03:44
                       )

                   [original:protected] => Array
                       (
                           [id] => 1
                           [rma_id] => 3
                           [item_id] => 8
                           [quo_id] => 0
                           [case_id] => i2eM20160120
                           [line_no] => 000001
                           [content] => test
                           [status] => sent
                           [read] => 0
                           [sender] => Tester
                           [created_at] => 2016-01-20 18:03:44
                           [updated_at] => 2016-01-20 18:03:44
                       )

                   [relations:protected] => Array
                       (
                       )

                   [hidden:protected] => Array
                       (
                       )

                   [visible:protected] => Array
                       (
                       )

                   [appends:protected] => Array
                       (
                       )

                   [fillable:protected] => Array
                       (
                       )

                   [guarded:protected] => Array
                       (
                           [0] => *
                       )

                   [dates:protected] => Array
                       (
                       )

                   [touches:protected] => Array
                       (
                       )

                   [observables:protected] => Array
                       (
                       )

                   [with:protected] => Array
                       (
                       )

                   [morphClass:protected] => 
                   [exists] => 1
               )

       )

)

它工作正常,并且显示只有一个关于id为1的项目的反馈.

我担心的是数据集在[attributes:protected][original:protected]中重复.这只是一个测试 case ,真正的 case 将包含数千条反馈,而拥有一个重复的数据集是对内存的巨大浪费.如果我使用DB::table('table_name')方法,数据集不会被复制,但这就不那么方便了.

为什么laravel需要复制模型中的数据?

有没有办法让它只返回一组数据?

目前,我使用->toArray()在查询后立即减少不必要的数据,但内存使用率仍然存在,因为laravel仍在创建该数据集.

推荐答案

虽然很难得到一个好的示例,但它允许您在确定保存属性之前设置属性.如果您完成了许多函数,并最终判断是否为最终保存正确设置了所有内容,而无需将所有内容存储在单独的变量中,那么这可能很好.

非常小的例子:

$user = User::find(1);
print_r($user);
$user->name = 'John Doe';
print_r($user);
$user->save();
print_r($user());

返回如下内容:

First print:

[attributes:protected] => Array
(
   [id] => 1
   [name] => 'Jimmy Doe'
   ...
)
[original:protected] => Array
(
   [id] => 1
   [name] => 'Jimmy Doe'
   ...
)

Second print:

[attributes:protected] => Array
(
   [id] => 1
   [name] => 'John Doe'
   ...
)
[original:protected] => Array
(
   [id] => 1
   [name] => 'Jimmy Doe'
   ...
)

Thrid print:

[attributes:protected] => Array
(
   [id] => 1
   [name] => 'John Doe'
   ...
)
[original:protected] => Array
(
   [id] => 1
   [name] => 'John Doe'
   ...
)

只有在save()之后,数据才真正被保存到数据库中.

当一个模型被保存()时,Eloquent 者的syncOriginal()会被激活:

/**
 * Sync the original attributes with the current.
 *
 * @return $this
 */
public function syncOriginal()
{
    $this->original = $this->attributes;

    return $this;
}

Laravel相关问答推荐

Hostinger Laravel网站未显示错误

Laravel带S3存储器

在GitHub操作中缺少PHPStan和PHPMD输出,但在本地存在

如何从@foreach 循环中捕获所有数据并将其传递给 值? - Laravel

在 Laravel 中清除 Routes&config:cache 后未定义的常量

使用 Laravel 计算页面浏览量

Laravel 5 - 更改模型文件位置

Laravel belongsTo 关系 - 试图获取非对象的属性

Eloquent: hasNot 带参数

用 laravel 表单确认删除?

localhost 和stream_socket_enable_crypto():SSL 操作失败,代码为 1

如何比较laravel中的两个加密(bcrypt)密码

Twilio 查找 API 不起作用?

从 Laravel Cashier 获取下一个账单日期

为什么客户凭证应该与 Laravel Passport 中的用户相关联?

Laravel 和 PHPStorm 项目的 gitignore 中包含什么?

获取多列字段?

Laravel 分页漂亮的 URL

Laravel 将附加参数传递给函数

限制自己重载外部 API 的速率