2

I'm a big ol' newbie at Laravel, and I'm trying to do a query scope but it doesn't seem to be working, I keep getting this error:

Argument 1 passed to Letters::scopeForUser() must be an instance of User

My user is logged in, but it still doesn't seem to be working.

This is my Letters model:

<?php

class Letters extends Eloquent {

    protected $table = 'letters';

    public function scopeForUser(User $u)
    {

        return $query->where('userid', '=', $u->id);

    }

}

and in my controller I have the following:

Route::get('myletters', array(
    'before' => 'auth|userdetail',
    function()
    {

        // Grab the letters, if any, for this user
        $letters = Letters::forUser(Auth::user())->get();

        $data = [
            'letters' => $letters
        ];

        return View::make('myletters', $data);

    }
));

Any help would be greatly appreciated.

1 Answer 1

2

You should pass a variable $query as the first argument to your method in the Model. For example:

public function scopeForUser($query, User $u)
{

    return $query->where('userid', '=', $u->id);

}

The first argument doesn't necessarily need to be $query, but it should be the same variable that you are using inside the scope method ($query in this case).

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.