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.
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).