Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Posts::actives()->get() returns an Eloquent Collection which happen to have an toArray() method.

Using Posts::actives()->first()->toCustomJS() should work since it calls toCustomJS() on a Posts model, not a collection.

0

yes but with this way, it only give me the first occurence... I need it for all

0
Solution

like @tkprocat said, you are getting this because you are only overriding your model class.

If you need to do for all, you would need to extend EloquentCollection class.

class YourCollection extends \Illuminate\Database\Eloquent\Collection {
   public function toCustomJS() {
      // Your own implementation goes here. 
   }
}

There is also a SO question here similar,

http://stackoverflow.com/questions/25880758/extending-collection-in-laravel

If you choose not to extend your collection, you could probably do this as well,

$posts = Posts::get(); 
$postsArray = []; 

foreach ($posts as $post) {
   array_push($postsArray, $post->toCustomJS()); 
}
Last updated 9 years ago.
0

Thank you! Just what I was looking for.

0

Sign in to participate in this thread!

Eventy

Your banner here too?

LonnyX lonnyx Joined 30 Mar 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.