BaseRepository.php 1.1 KB
<?php

namespace Uema;

use Uema\BaseModel;

class BaseRepository
{
    protected $model;

    public function __construct(BaseModel $model)
    {
        $this->model = $model;
    }

    public function create(array $data)
    {
        return $this->model->create($data);
    }

    public function update(array $data, $id, $attribute = null)
    {
        if (!$attribute) {
            $attribute = $this->model->getKeyName();
        }

        $collection = $this->model->where($attribute, '=', $id)->get();

        if ($collection) {
            foreach ($collection as $obj) {
                $obj->fill($data)->save();
            }

            return $collection->count();
        }

        return false;
    }

    public function delete($id)
    {
        return $this->model->destroy($id);
    }

    public function all()
    {
        return $this->model->all();
    }

    public function lists($identifier, $field, $sort = 'desc')
    {
        return $this->model->orderBy($identifier, $sort)->pluck($field, $identifier);
    }

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