Storm Spirit Storm Spirit - 2 months ago 7
PHP Question

How to initiate model without get() on laravel

I have this on my model

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'];

}


then this is on my Controller

$scholars = new Scholar; <------

if(Input::get('age_from'))
$scholars->where('age', '=', Input::get('age_from'));

$scholars->get();

return $scholars;


I want to initiate this, but when I try the
$scholars = Scholar::all();
I cant use
->get()
anymore;

Anyway how to do this optional
where
?

Answer

You can use newQuery;

$scholars = (new Scholar)->newQuery();

if(Input::get('age_from')) {
    $scholars->where('age', '=', Input::get('age_from'));
}

return $scholars->get();
Comments