Skip to main content
deleted 101 characters in body; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Ok I just started using Laravel 4,. I read some tutorials on how to implement CRUD in laravel. And finallyLaravel, and I was able to develop a portion of it. What I am going to show you is more on Controller and Eloquent ORM.

I have here 2 models "Group" which can only have one "Country"Country, but "Country"Country can be assigned to different "Group"Group ("one to many").

class Group extends Eloquent
{
    protected $table = 'group';
    protected $guarded = array('group_id');
    protected $primaryKey = 'group_id';
    
    public function country() 
    {
        return $this->belongsTo('country', 'country_id');
    } 
    
}
class Country extends Eloquent
{
    protected $table = 'country';
    protected $primaryKey = 'country_id';
    
    public function group() {
        return $this->hasMany('group', 'country_id');
    }
    
}
public 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");
        
    // get all the 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 for healthy response guys.

Thanks in advance!

Ok I just started using Laravel 4, I read some tutorials on how to implement CRUD in laravel. And finally I was able to develop a portion of it. What I am going to show you is more on Controller and Eloquent ORM.

I have here 2 models "Group" which can only have one "Country", but "Country" can be assigned to different "Group" ("one to many")

class Group extends Eloquent
{
    protected $table = 'group';
    protected $guarded = array('group_id');
    protected $primaryKey = 'group_id';
    
    public function country() 
    {
        return $this->belongsTo('country', 'country_id');
    } 
    
}
class Country extends Eloquent
{
    protected $table = 'country';
    protected $primaryKey = 'country_id';
    
    public function group() {
        return $this->hasMany('group', 'country_id');
    }
    
}
public 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");
        
    // get all the 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 for healthy response guys.

Thanks in advance!

I just started using Laravel 4. I read some tutorials on how to implement CRUD in Laravel, and I was able to develop a portion of it. What I am going to show you is more on Controller and Eloquent ORM.

I have here 2 models "Group" which can only have one Country, but Country can be assigned to different Group ("one to many").

class Group extends Eloquent
{
    protected $table = 'group';
    protected $guarded = array('group_id');
    protected $primaryKey = 'group_id';
    
    public function country() 
    {
        return $this->belongsTo('country', 'country_id');
    } 
}
class Country extends Eloquent
{
    protected $table = 'country';
    protected $primaryKey = 'country_id';
    
    public function group() {
        return $this->hasMany('group', 'country_id');
    }
}
public 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");
        
    // get all the 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);
}
Source Link

CRUD implementation using Laravel 4

Ok I just started using Laravel 4, I read some tutorials on how to implement CRUD in laravel. And finally I was able to develop a portion of it. What I am going to show you is more on Controller and Eloquent ORM.

I have here 2 models "Group" which can only have one "Country", but "Country" can be assigned to different "Group" ("one to many")

#Group Model

class Group extends Eloquent
{
    protected $table = 'group';
    protected $guarded = array('group_id');
    protected $primaryKey = 'group_id';
    
    public function country() 
    {
        return $this->belongsTo('country', 'country_id');
    } 
    
}

#Country Model

class Country extends Eloquent
{
    protected $table = 'country';
    protected $primaryKey = 'country_id';
    
    public function group() {
        return $this->hasMany('group', 'country_id');
    }
    
}

In my controller, I have index() function which accepts user search filter and pagination request.

#Group Controller

public 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");
        
    // get all the 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 for healthy response guys.

Thanks in advance!