문제

I have a model called Entry which has many fields (fields is another table, with the model Field and with entry_id as one of it's columns - linked to the Entry model by the hasMany relationship):

class Entry extends \Eloquent {
    public function fields() {
        return $this -> hasMany('Field');
    }
}

I'm trying to get all the fields that belong to a collection for entries, but if I try

Entry::where('form_id', $formId) -> fields
// There are multiple entries for each form_id

it throws up an error

Undefined property: Illuminate\Database\Eloquent\Builder::$fields

Trying to do it on the collection doesn't work either

Entry::where('form_id', $formId) -> get() -> fields

Undefined property: Illuminate\Database\Eloquent\Collection::$fields

Trying the same for just the first record, works as expected

Entry::where('form_id', $formId) -> first() -> fields

So, is there an eloquent way to do this?

For now, all I can think of is looping through all the entries and manually merging their fields, or something like

$entries = Entry::where('form_id', $formId) -> get();
$entryIDs = array();
foreach($entries as $entry) {
    $entryIDs [] = $entry -> id;
}
$fields = Field::whereIn('entry_id', $entryIDs);
도움이 되었습니까?

해결책

It looks like your relations are like this: Form hasMany (or hasOne) Entry hasMany Field, right? So you can use a shorthand, that Eloquent provides, for what you are trying to do:

// Form model
public function fields()
{
   return $this->hasManyThrough('Field', 'Entry');
}

This way you get all the fields you want providing the Form model, as easy as this:

$fields = Form::find($formId)->fields; // Collection of Field models

Add constraints to the fields:

$fields = Form::find($formId)->fields() // return query builder object...
               ->where('entry_id', '=', 'value')  // ... so you can chain any builder method
               ->orderBy('something') // for example order by
               ->get();               // don't forget to return results with get()

edit 2

The easiest way to get both fields and entries, according to your comment, would be:

1 Load fields that match outer where and entry for each fields matching the inner where clause:

$fields = $form->fields()->where('x','=','value') // filter fields
              ->with(['entry' => function ($q) use ($entriesConstraint) {
                   $q->where('entriesColumn', '=', $entriesConstraint);
              }])->get();

2 Load fields that match outer where and have related entry matching inner where (and entry for each field):

$fields = $form->fields()->where('x','=','value') // filter fields
              ->whereHas('entry', function ($q) use ($entriesConstraint) {
                   $q->where('entriesColumn', '=', $entriesConstraint);
              })->with('entry')
              ->get();

whereHas and with mix:

User::whereHas('categories', function ($q) {

   $q->where('name','like','%lo%');  // first constraint to fetch entries
})->with(['categories'=>function($q){

   $q->where('name','like','%o%')    // another constraint for processing related model
     ->selectRaw('categories.id as id,avg(categories.id) as avg')   // raw for fancy aggregate
     ->groupBy('category_user.user_id');   // group by for aggregate
}])->get();

다른 팁

One possible solution:

Get all/specified entries and eager load the fields, then loop over them

$fields = Entry::with(['fields'])->where('form_id', $formId)->get()->lists('fields');

foreach ($fields AS $field) { var_dump($field->toArray()); }
Entry::where('form_id', $formId) -> fields

should we rewritten has follow:

$entries = Entry::where('form_id', $formId)->with('fields')->get();

You will have a collection of Entry with their fields. You can use lists after, has Dane said.

So it looks like you're trying to select a single column and get the result as an array.

It looks like QueryBuilder has a way of doing this

$roles = DB::table('roles')->lists('title');

But Eloquent doesn't seem to, at least not out of the box. However, you could take your Collection, use ->toArray(), and then PHP's array_column if you're using PHP >= 5.5.0

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top