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.
yes but with this way, it only give me the first occurence... I need it for all
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());
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community