如何启动模式不会对laravel
的get()我有这个在我的模型Scholar
如何启动模式不会对laravel
<?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'];
}
,那么这是对我的控制器
$scholars = new Scholar; <------
if(Input::get('age_from'))
$scholars->where('age', '=', Input::get('age_from'));
$scholars->get();
return $scholars;
我要发起这个,但是当我尝试$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();
也许这是不是做的最好的方式,但它的工作原理
试着这么做:
$scholars = Scholar::where('id','>',0); //Assuming you have an autoincrement id in your table
if(Input::get('age_from'))
$scholars->where('age', '=', Input::get('age_from'));
$scholars->get();
return $scholars;
如果你需要得到所有的学者,反正你可以得到所有行的集合中只一个查询:
$allScholars = Scholar::all();
然后你可以从收集的任何数据:
$scholars = $allScholars->where('age', '=', Input::get('age_from'));
现在你们所有的学者在$allScholars
收藏和指定年龄的学者$scholars
之一。
我想使用砍砍查询...因为我还有其他查询 –
你能解释一下你究竟是什么意思? –
使用'all()'选择所有数据,然后'where()'过滤效率低下的集合。您将表中的所有数据加载到内存中,然后通过结果进行筛选。 –
它返回我这个'非静态方法Illuminate \ Database \ Eloquent \ Model :: newQuery()不应该被静态调用,假设$ this来自不兼容的上下文' –
啊,道歉,更新 –