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

Not sure if this is the cleanest approach, but I made it work by returning a collection.

use Illuminate\Support\Collection; public function getFooBarAttribute() { return new Collection(array('foo','bar')); }

mattsears18 said:

The next to last code snippet on this page (http://laravel.com/docs/eloquent) shows how to define custom attributes on a model. Can a custom attribute return an array? For example:

public function getFooBarAttribute() { return array('Foo', 'Bar'); }

I get "Array to string conversion" errors when I try this. Any ideas? The array that I'm trying to return is a list of files in a directory that are in no way referenced in a database.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

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.