0

Here is my query:

return $this->mGalleries->with(array('pictures' => function($query){
        $query->wherePublished(1)->orderBy('sort');
    }))->whereInUse(1)->wherePublished(1)->orderBy('date')->get(array('id', 'title'))->toArray();

I have looked at a question here but no luck.

I have tried the suggested:

 $query->select('title')->wherePublished(1)->orderBy('sort');

But all of the fields continue to come out, although title does appear first when I do this.

Any ideas on how to fix this?

Community
  • 1
  • 1
panthro
  • 22,779
  • 66
  • 183
  • 324
  • What is the problem? You just said you've tried the suggested and fields continue to come out, and how to fix it, but you don't ask a clear question – Steve Bauman Feb 12 '14 at 15:25
  • `$query->select('title')->wherePublished(1)->orderBy('sort');` should wotk but `id/primary key` must be in the select list. – The Alpha Feb 12 '14 at 16:35

1 Answers1

0

You only want to use some columns of the returned set if I understand your question correctly..

I never use select on eloquent models because misuse can break the relations between models (necessary columns I.E. picture_id are needed to find the picture). So this makes the outcome of the query misleading.

Why do not use it like this:

// $galleries = Galleries->with....
$galleries = Gallery::with(array('pictures' => function($query){
        $query->wherePublished(1)->orderBy('sort');
}))->whereInUse(1)
   ->wherePublished(1)
   ->orderBy('date')
   ->get()
   ->toArray();

And then when you need for example the title for the first one: $falleries[0]['title'].

You can add the protected $hidden = array('field/attr', ..); property in your eloquent model if you want to hide specific attributes from the toArray() function.

Sven van Zoelen
  • 6,989
  • 5
  • 37
  • 48