1

In my controller I have:

$Locations = Locations::where(something);       
$Locations->get()->toArray(true);

And inside the model:

function toArray($include_all = false) {
   var_dump($include_all);
}

The include all variable is false, although the function gets called. Is there a reason why it's doing that ?

I want to call a custom toArray because I have more oneToMany relations with different structures that I want to change (some of them are serialized for example)

Thank you

2 Answers 2

3

You can use Illuminate\Support\Collection methods such as map() and filter() to modify the collection and at the end of that call toArray() method.

Sign up to request clarification or add additional context in comments.

5 Comments

Yes, but then I would have to use the ->map() every time I do the database query, I wanted to do it inside the toArray() function of the model so I wont have to duplicate the code
you can write a function that has all logic, for example: function myFunc($arg) { return // mapping logic} Model::someQuery()->get()->map('myFunc')->toArray(); Job of toArray() is only convert collection of items to array, no more.
It should also be noted that ->get always returns an instance of Illuminate\Support\Collection. Furthermore, ->toArray() is a method provided by Illuminate\Support\Collection and therefore overwriting it does not seem like a sane approach. The mapping does seem like the correct way to go here.
And how can I change an array value inside the map function? I see that it works to change a string value, but not an array
You can and able to change and modify any data type in map functions, and return modified value inside the function.
0

It would be fairly easy to overwrite, however I think there is some confusion that should be cleared up first.

First, the toArray() method you are calling in this case is on the Collection which is the object which is returned when you use get() on your model.

With that said, you can add the following to your Location model to return a custom collection...

public function newCollection(array $models = [])
{
    return new CustomCollection($models);
}

Then you write the new CustomCollection class with appropriate namespaces just to make sure it gets auto loaded fine, have it extend \Illuminate\Database\Eloquent\Collection and then you can proceed to override the toArray method.

However, it feels like you randomly selected this toArray() as a proper candidate to perform your logic just because you are already using it. You should think about creating a new function which calls $this->toArray() to grab the results and modify them as you need and return that.

If you need this same functionality on other models, just keep adding that newCollection method where needed.

This is also in the docs as well, might be worth checking out...

https://laravel.com/docs/5.2/eloquent-collections#custom-collections

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.