2016-09-21 57 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); 
} 
相關問題