Skip to content
On this page

Services

Services are also known as the Business Layer, contains the functionality that compounds the core of the application, thus becoming highly reusable. It deals with all the business logic (data retrieval, updates, deletions, and so on).

It uses inheritance property of OOP programming approach and uses base service.

How to use service

Below is the basic example that shows how services are used:

use App\Services\Service;

class UserSevice extends Service {
    public function __construct(User $user)
    {
        parent::__construct($user);
    }

    public function getAllData($data, $selectedColumns = [], $pagination = true)
    {
        $query = $this->query();
        if (isset($data->keyword) && $data->keyword !== null) {
            $query->where('name', 'LIKE', '%'.$data->keyword.'%');
        }
        if (count($selectedColumns) > 0) {
            $query->select($selectedColumns);
        }
        if ($pagination) {
            return $query->orderBy('id', 'DESC')->paginate(PAGINATE);
        }

        return $query->orderBy('id', 'DESC')->get();
    }
}

Model that is to be used is send in super function inside the constructor. A basic example of querying information using model from the database is shown in the function above. Here, this->query specifies the model "user" passed in the super function. It is one of the implementation showing the basic business logic.