Skip to content

Instantly share code, notes, and snippets.

@muratsplat
Last active August 29, 2015 14:15
Show Gist options
  • Save muratsplat/dd1efb3735caf7f8c6c0 to your computer and use it in GitHub Desktop.
Save muratsplat/dd1efb3735caf7f8c6c0 to your computer and use it in GitHub Desktop.

Revisions

  1. muratsplat renamed this gist Feb 11, 2015. 1 changed file with 0 additions and 0 deletions.
  2. muratsplat revised this gist Feb 11, 2015. No changes.
  3. muratsplat created this gist Feb 11, 2015.
    108 changes: 108 additions & 0 deletions Example Repository Abstract Clas
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,108 @@
    <?php namespace Boruu\Root\Mvc\Repository;


    /**
    * Core Repository for Eloquent
    *
    * @author Murat Ödünç <[email protected]>
    * @copyright (c) 2015, Murat Ödünç
    * @license http://www.gnu.org/licenses/gpl-3.0.html GPLv3
    * @package Repository
    */
    abstract class EloquentRepository {

    /**
    * Eloquent Model
    *
    * @var \Illuminate\Database\Eloquent\Model
    */
    protected $model;

    /**
    * current model
    *
    * @var \Illuminate\Database\Eloquent\Model
    */
    protected $current;

    /**
    * Constructer
    */
    public function __construct($model = null) {

    $this->model = $model;
    }

    /**
    * to get all users in a collection object
    *
    * @return \Illuminate\Database\Eloquent\Collection
    */
    public function all() {

    return $this->model->all();
    }

    /**
    * to get model by id
    *
    * @param int $id
    * @return Illuminate\Database\Eloquent\Model|null
    */
    public function getById($id) {

    return $this->model->find($id);
    }

    /**
    * To create model
    *
    * @param array $attributes
    * @return bool
    */
    abstract public function create(array $attributes);

    /**
    * To update model
    *
    * @param int $id
    * @param array $attributes
    * @return boolean
    */
    abstract public function update($id, array $attributes);

    /**
    * to detete model
    *
    * @param int $id
    * @return bool
    */
    public function delete($id) {

    $this->current = $this->getById($id);

    return is_null($this->current) ? false : $this->current->delete();
    }

    /**
    * To get all users in Collection object
    *
    * @return \Illuminate\Database\Eloquent\Collection
    */
    public function getModels() {

    return $this->all();
    }

    /**
    * to get Instance
    *
    * @return \Illuminate\Database\Eloquent\Model
    */
    public function getCurrent() {

    return $this->current;
    }


    }