Sto*_*rit 2 php model laravel eloquent
我的模型上有这个Scholar
<?php namespace App;
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'];
}
Run Code Online (Sandbox Code Playgroud)
那么这是我的控制器上
$scholars = new Scholar; <------
if(Input::get('age_from'))
$scholars->where('age', '=', Input::get('age_from'));
$scholars->get();
return $scholars;
Run Code Online (Sandbox Code Playgroud)
我想启动这个,但是当我尝试时$scholars = Scholar::all();我无法->get()再使用;
无论如何如何做这个可选的where?
您可以使用newQuery;
$scholars = (new Scholar)->newQuery();
if(Input::get('age_from')) {
$scholars->where('age', '=', Input::get('age_from'));
}
return $scholars->get();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2167 次 |
| 最近记录: |