lyre/lyre

One minute CRUD with a simple command to create all resources

2.0.0 2025-05-02 09:20 UTC

README

Lyre

License

About Lyre

Lyre is a php package built for Laravel. Lyre works together with the rich Laravel ecosystem, and goes hand in hand with its philosophy of expressive, elegant syntax. It takes enjoyable and creative development a step further and makes you feel the harmony of it all as the elements come together. Lyre utilizes the following to create the rhythm of the future:

Lyre is accessible, powerful, and it is your next favorite tool.

Get started right away

composer require lyre/lyre
  • Add LyreServiceProvider to your providers array under bootstrap > providers.php
  • Add use BaseModelTrait to your existing models.
  • Run php artisan vendor:publish --provider="Lyre\Providers\LyreServiceProvider" to publish Lyre configuration.
  • Clear configuration cache
php artisan lyre:all Post
  • Add your columns to your migration and migrate

  • Enable API routing (If using Laravel > 10)

php artisan install:api
  • Add your model to your routes file
Route::apiResource('posts', PostController::class);
  • Consume your API!

  • Guess what? That's it.

Dependencies

Digging Deeper

RESTFULNESS

  • Lyre is naturally RESTFUL, meaning that after adding your apiResource or resource in your routes file, you will be able to create, update, and delete all records using these routes, borrowed from the above example:

     GET|HEAD        posts
     POST            posts
     GET|HEAD        posts/{post}
     PUT|PATCH       posts/{post}
     DELETE          posts/{post}
    

Hidden Gem

  • Lyre comes with a bulkUpdate option that also follows the RESTFUL convention for batch operations, allowing you to efficiently update multiple records in a single request.
  • All you need to do is comma separate the values in your PUT|PATCH request.
  • For example:
Single Update
   posts/1
Bulk Update
   posts/1,2,3

Filters

Easily Handle Relationships

  • To return a model with all its relationships, simply chain a with method that takes an array of relations when querying the repository like so:
$data = $this->postRepository->with(['author', 'comments'])->all();
  • All you need to do is define the relationships in your model:
public function author()
{
       return $this->belongsTo(User::class, 'user_id');
}

public function comments()
{
       return $this->hasMany(Comment::class);
}
  • Then override the loadResources method of the Posts resource in app>Http>Resources>Post
public static function loadResources(): array
{
       return [
              'author' => User::class,
              'comments' => Comment::class
       ];
}
  • Now you will be able to get your model with these relationships using a simple query string

     posts/1?with=author,comments
    

Filters

Column Filters

  • Easily return data filtered by a specific column
$data = $this->postRepository->columnFilters(['status' => 'active'])->all();

Range Filters

  • Easily filter your data by range, for example, created_at!
$data = $this->postRepository->rangeFilters(['created' => [now()->subHours(24), now()])->all();

Relation Filters

  • You can even return your data filtered by specific relationships!
$data = $this->postRepository->relationFilters('author' => 'id,1')->all();

Search Query

  • Search through your repository!
$data = $this->postRepository->searchQuery(['search' => 'lyre'])->all();

Method Chaining

  • What is more? You can chain all these methods to fine tune your query!
$data = $this->postRepository->with(['author', 'comments'])
       ->columnFilters(['status' => 'active'])
       ->rangeFilters(['created' => [now()->subHours(24), now()])
       ->relationFilters('author' => 'id,1')
       ->searchQuery(['search' => 'lyre'])
       ->all()

API Query Strings

Lyre provides the following query string filters to filter all your data the way you want!

  • with - A comma separated list of all the relationships that you want to return in your response
  • paginate - This boolean value determines whether pagination is set, default is true
  • page - Changes the current page in a paginated request
  • per_page - Changes the number of items returned in the request
  • latest - Returns the latest value items
  • order - Returns ordered items, e.g. /subjects?order=name,asc
  • relation - Filter by a column in a related table, i.e. /subjects?relation=courses,english returns only the Subjects that belong to an English course
  • search - Search through all columns for a string match, e.g. /subjects?search=physics
  • startswith - Get all rows whose NAME_COLUMN startswith substring, e.g. /subjects?startswith=b
  • withcount - Get the count of a relationship, e.g. /subjects?withcount=tasks returns with a tasks_count field containing the number of tasks for each subject.

Known Issues

Installation

  • All models must use BaseModelTrait, otherwise throws error: Call to a member function connection() on null
  • Fails to publish stubs, creates empty folder. Stubs must be copied from STUBS

Collaboration

       git tag x.x.x
       git push origin x.x.x

USING SPATIE

Using Activity Log

Publish the migration with:

php artisan vendor:publish --provider="Spatie\Activitylog\ActivitylogServiceProvider" --tag="activitylog-migrations"

Using Roles and Permissions

Publish the migrations and the configs with:

php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider"

Add the Spatie\Permission\Traits\HasRoles trait to your User model(s):

use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable
{
    use HasRoles;

    // ...
}

License

Lyre is open-sourced software licensed under the MIT license.