It is convenient that laravel model provides a method that it can return results from another associated table.
For example, I have a table called item and another table called feedback, where the feedback table stores feedback of an item in the item table. So, to get the all feedback of item with id 1, I will do:
Item::find(1)->feedback;
And the following this the printout of the object returned.
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
)
)
)
It works fine, and it shows that there is only one feedback on item with id 1.
What concerns me is that the dataset is duplicated in [attributes:protected]
and [original:protected]
. This is just a testing case and the real case will consist of thousands of feedback and having a duplicated dataset is a huge waste of memory. The dataset is not duplicated if I am using the DB::table('table_name')
approach, but that is much less convenient.
Why does laravel need to duplicate the data in model?
And is there a way to make it return only one set of data?
Currently I am using ->toArray()
to trim down the unnecessary data right after the query, but the memory usage is still there because laravel is still creating that set of data.