CRUD implementation using Laravel 4 (code review) -
CRUD implementation using Laravel 4 (code review) -
i not sure if allowed here inquire code review, i'll give seek code improvements.
ok started using laravel 4, read tutorials on how implement crud in laravel. , able develop portion of it. going show more on controller , eloquent orm.
i have here 2 models "group" can have 1 "country", "country" can assigned different "group" ("one many")
group modelclass grouping extends eloquent { protected $table = 'group'; protected $guarded = array('group_id'); protected $primarykey = 'group_id'; public function country() { homecoming $this->belongsto('country', 'country_id'); } }
country model class country extends eloquent { protected $table = 'country'; protected $primarykey = 'country_id'; public function group() { homecoming $this->hasmany('group', 'country_id'); } }
in controller, have index() function accepts user search filter , pagination request.
group controllerpublic function index() { // search variables here $s_name = input::get("s_name"); $s_status = input::get("s_status"); // order variables here $sortby = input::get("sort_by"); $orderby = input::get("order_by"); // groups $groups = group::with('country') ->join('country', 'country.country_id', '=', 'group.country_id'); // search conditions $groups = $groups->wherein('group_status', array(active, inactive)); if (!empty($s_name)) { $groups = $groups->where('group_name', 'like', "%$s_name%"); } if (!empty($s_status)) { $groups = $groups->where('group_status', 'like', "%$s_status%"); } // order if (!empty($sortby)) { $orderby = (empty($orderby)) ? ascending : $orderby; if ($sortby == "o_name") { $groups = $groups->orderby('group_name', $orderby); } else if ($sortby == "o_country") { $groups = $groups->orderby('country_short_name', $orderby); } else if ($sortby == "o_status") { $groups = $groups->orderby('group_status', $orderby); } } else { $groups = $groups->orderby('group.created_at', descending); } $groups = $groups->paginate(pagination_limit); }
hoping healthy response guys. in advance!
laravel-4
Comments
Post a Comment