我的模型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'];
}
然后这是我的控制器
$scholars = new Scholar; <------
if(Input::get('age_from'))
$scholars->where('age', '=', Input::get('age_from'));
$scholars->get();
return $scholars;
我想发起这个,但是当我尝试$scholars = Scholar::all();
我再也不能使用->get()
时;
无论如何如何做这个可选的where
?
答案 0 :(得分:3)
您可以使用newQuery;
$scholars = (new Scholar)->newQuery();
if(Input::get('age_from')) {
$scholars->where('age', '=', Input::get('age_from'));
}
return $scholars->get();
答案 1 :(得分:1)
如果您需要获得所有学者,只需一个查询即可获得集合中的所有行:
$allScholars = Scholar::all();
然后你可以从集合中获取任何数据:
$scholars = $allScholars->where('age', '=', Input::get('age_from'));
现在$allScholars
$scholars
收集的所有学者和[HttpPost]
public ActionResult Login(User user, string returnUrl)
{
// Lets first check if the Model is valid or not
if (ModelState.IsValid)
{
using (MyDBContext context = new MyDBContext())
{
string username = user.Email;
byte[] password = user.Password;
(...)
中指定年龄的学者。
答案 2 :(得分:0)
可能这不是最好的方法,但它有效
尝试类似:
$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;