0

Why do I get an empty collection result using the scope? This is my current controller:

class PunxController extends Controller {
    public function index()
    {
        $scholars = Scholar::age()->get();

        return $scholars;
    }
}

and on my model Scholar:

use Illuminate\Database\Eloquent\Model;

class Scholar extends Model {

    protected $primaryKey = 'scholar_id';
    protected $fillable = ['ngo_id','scholar_lname','scholar_fname','scholar_image','scholar_nickname','scholar_cityAddress','scholar_permaAddress','scholar_birthday','scholar_placebirth','scholar_age','scholar_gender','scholar_email','scholar_contact'];

    public static function scopeAge($query)
    {
        return $query->where('scholar_age', '>=', 5);
    }
}

and the result is:

enter image description here

but when I try on PHPMYADMIN:

enter image description here

Update1

result of DB::getQueryLog():

enter image description here

4
  • Try running \DB::enableQueryLog() before you run the query and then use dd(\DB::getQueryLog()) after to see what was actually run. Then post the result here please! Commented Sep 21, 2016 at 16:24
  • @Jonathon here it is, check the update Commented Sep 21, 2016 at 16:28
  • Thanks. That query looks right. Which tells me that it's possible not looking at the right database. Are you running your webserver using a VM like vagrant? Commented Sep 21, 2016 at 16:53
  • @Jonathon I'm just running WAMP Commented Sep 21, 2016 at 17:18

2 Answers 2

2

Remove the static keyword from your method. Scopes shouldn't be static.

public function scopeAge($query)
{
    return $query->where('scholar_age', '>=', 5);
}

I'm guessing you would like to provide 5 as a parameter to the scope rather than hard-coding it. In which case you can do:

public function scopeAge($query, $age)
{
    return $query->where('scholar_age', '>=', $age);
}

And call it like

Scholar::age(5)->get();
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

public function scopeAge($query)
{
    return $query->where('scholar_age', '>=', 5);
}

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.