2016-09-21 149 views
0

为什么使用范围得到空的收集结果?这是我目前的控制器:为什么我的模型范围无效并返回一个空数组?

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

     return $scholars; 
    } 
} 

,并在我的模型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); 
    } 
} 

,其结果是:

enter image description here

但是当我尝试在PHPMYADMIN

enter image description here

UPDATE1

DB::getQueryLog()结果:

enter image description here

+0

尝试运行'\ DB :: enableQueryLog()'运行查询之前,然后使用'DD(\ DB :: getQueryLog()),看看实际运行的是什么。然后请在这里发布结果! – Jonathon

+0

@Jonathon在这里,检查更新 –

+0

谢谢。该查询看起来正确。这告诉我可能没有看到正确的数据库。你是否使用像流浪者一样的虚拟机来运行你的网络服务器? – Jonathon

回答

2

从您的方法中删除static关键字。范围不应该是静态的。

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

我猜你想提供5作为参数的范围,而不是硬编码。在这种情况下,你可以这样做:

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

,并调用它像

Scholar::age(5)->get(); 
0

试试这个:

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