0

The following code I have is working perfectly fine, however, it returns more data than what is necessary from each table:

public function getIndex()
    {
        $alerts = Criteria::with('bedrooms', 'properties')
        ->where('user_id', '=', Auth::id())
        ->get();

        $this->layout->content = View::make('users.alert.index', 
            array('alerts' => $alerts));
    }

What I'd like to do is, for example, select only the bedroom column out of the bedroom table. As it stands now, it returns all columns.

I have tried:

public function getIndex()
    {
        $alerts = Criteria::with('bedrooms' => function($q){
            $q->select('bedroom');
        }, 'properties')
        ->where('user_id', '=', Auth::id())
        ->get();

        $this->layout->content = View::make('users.alert.index', 
            array('alerts' => $alerts));
    }

But I am presented with the following error:

syntax error, unexpected '=>' (T_DOUBLE_ARROW)

Any help as to how I can achieve this will be hugely appreciated.


Update

public function getIndex()
{
    $alerts = Criteria::with(
        ['coordinate' => function($w){
            $w->select('name', 'id');
        }],
        ['bedrooms' => function($q){
            $q->select('bedroom', 'criteria_id');
        }]
        , 'properties')
    ->where('user_id', Auth::id())
    ->get();

    $this->layout->content = View::make('users.alert.index', 
        array('alerts' => $alerts));
}

The correct select query works for which ever is queried first. If I swap the queries around, the bedroom function works correctly, but the rest aren't eager loaded nor does the select query work.

3
  • 1
    Just a note, you might want to use Auth::user()->id instead of Auth::id() to get the current user's ID. Commented Jan 8, 2015 at 13:59
  • @msturdy, noted and changed. Many thanks. Commented Jan 8, 2015 at 14:00
  • @msturdy definitely not. If you don't have the user loaded, then it will run redundant query, while Auth::id() is the way to go if you need only the id. Commented Jan 8, 2015 at 14:18

2 Answers 2

1

Just pass an array there:

// note [ and ]
$alerts = Criteria::with(['bedrooms' => function($q){
        $q->select('bedroom', 'PK / FK');
    }, 'properties'])

Also mind that you need to select the keys of that relation (primary key/foreign key of the relation).

Sign up to request clarification or add additional context in comments.

3 Comments

Will this work for more than one table inside the with function? I've added a query on to the properties but it still seems to be selecting all columns. Also, the eager loading of the other tables appears to have gone?
Please see the updated section @Jarek, it doesn't appear to work when I add the function to more than one table.
Yeah, the closing parenthesis was misplaced. You need to pass single array with multiple keys/closures. Check the edit.
0

The answer to your update question is that you need to eager load other values in the same array some thing like this.

public function getIndex()
{
    $alerts = Criteria::with(
        ['coordinate' => function($w)
        {
            $w->select('name', 'id');
        },
        'bedrooms' => function($q)
        {
            $q->select('bedroom', 'criteria_id');
        }
        , 'properties'])
    ->where('user_id', Auth::id())
    ->get();

    $this->layout->content = View::make('users.alert.index', 
        array('alerts' => $alerts));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.