定制 charleslightjarvis/laravel-todo 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

charleslightjarvis/laravel-todo

Composer 安装命令:

composer require charleslightjarvis/laravel-todo

包简介

A Laravel package to attach todo lists to any Eloquent model.

README 文档

README

Latest Version on Packagist Total Downloads License

Attach todo lists to any Eloquent model in your Laravel application.

The Problem

In a Laravel application, multiple entities (User, Project, Team, Invoice...) often need their own todo list. Without a package, you have to:

  • Create a separate table for each entity type (user_todos, project_todos, team_todos...)
  • Duplicate models, scopes, and relationships
  • Maintain the same logic across multiple places
  • No unified API to interact with todos

The Solution

laravel-todo lets you attach todos to any Eloquent model with a single trait. One table, one logic, every model.

  • ✅ Attach todos to User, Project, Team, Invoice — anything
  • ✅ Fluent API via Trait and Facade
  • ✅ Built-in scopes: pending, completed, overdue, highPriority, dueToday
  • ✅ Polymorphic creator support (tracks who created the todo)
  • ✅ Zero duplication

Features

  • ✅ Polymorphic relationship – attach todos to any model (User, Project, Team, etc.)
  • ✅ Fluent API via Facade or Trait
  • ✅ Built-in scopes: pending(), completed(), overdue(), highPriority(), dueToday()
  • ✅ Status management: pending, in_progress, completed, cancelled
  • ✅ Priority levels: low, medium, high
  • ✅ Tracks who created each todo (polymorphic creator relation)
  • ✅ Zero UI – backend only, integrate however you want

Requirements

  • PHP 8.2 or higher
  • Laravel 11.0 or higher

Installation

Install the package via Composer:

composer require charleslightjarvis/laravel-todo

Publish the migration file:

php artisan vendor:publish --tag="todo-migrations"

Run the migrations:

php artisan migrate

Publish the configuration file (optional):

php artisan vendor:publish --tag="todo-config"

Configuration

The config file config/todo.php allows you to customize:

return [
    'prune_after_days' => 30,

    'models' => [
        'todo' => CharlesLightjarvis\Todo\Models\Todo::class,
    ],

    'todo_morph_key' => 'todoable_id',
];

Usage

1. Add the trait to your model

Add HasTodos to any Eloquent model you want to attach todos to:

use CharlesLightjarvis\Todo\Traits\HasTodos;

class User extends Model
{
    use HasTodos;
}

class Project extends Model
{
    use HasTodos;
}

2. Creating todos via the Trait

Use the todos() relation directly on any model that uses HasTodos:

$user = User::find(1);

// Create a todo on the model
$todo = $user->todos()->create([
    'title'    => 'Buy groceries',
    'priority' => 'high',
    'due_at'   => now()->addDays(2),
]);

// Create with addTodo() — optionally assign a creator
$todo = $user->addTodo([
    'title'    => 'Finish the report',
    'priority' => 'medium',
], $creator);

3. Querying todos via the Trait

All built-in scopes are available directly on the relation:

$user->todos()->pending()->get();
$user->todos()->inProgress()->get();
$user->todos()->completed()->get();
$user->todos()->cancelled()->get();

$user->todos()->highPriority()->get();
$user->todos()->overdue()->get();
$user->todos()->dueToday()->get();

// Scopes can be chained
$user->todos()->pending()->highPriority()->get();

4. Creating and querying todos via the Facade

The Todo facade provides a model-agnostic API — useful when you do not have a direct reference to the owning model instance:

use CharlesLightjarvis\Todo\Facades\Todo;

// Create a todo for any model
$todo = Todo::createFor($user, [
    'title'    => 'Fix navigation bug',
    'priority' => 'high',
    'due_at'   => now()->addWeek(),
]);

// Scope queries to a specific model
Todo::for($user)->pending()->get();
Todo::for($user)->highPriority()->get();
Todo::for($user)->overdue()->get();

// Count
Todo::for($user)->count();
Todo::for($user)->pending()->count();
Todo::for($user)->completed()->count();

5. Completing and cancelling todos

Both methods are ownership-aware — they silently return false if the todo does not belong to the model:

// Complete a todo
$user->completeTodo($todo);

// After completion
$todo->refresh();
$todo->status->value;  // 'completed'
$todo->completed_at;   // Carbon timestamp

// Cancel a todo
$user->cancelTodo($todo);

$todo->refresh();
$todo->status->value;  // 'cancelled'

// Another user cannot complete a todo they don't own
$otherUser->completeTodo($todo); // returns false, status unchanged

6. Accessing todo relations

// All todos attached to the model
$user->todos;

// All todos created by the model (via the creator relation)
$user->createdTodos;

7. Tracking who created a todo

// Via addTodo — pass the creator as the second argument
$todo = $project->addTodo(['title' => 'Review PR'], auth()->user());

// Or set creator fields manually
$todo = $project->todos()->create([
    'title'        => 'Review PR',
    'creator_type' => $user->getMorphClass(),
    'creator_id'   => $user->id,
]);

// Resolve the creator
$todo->creator; // returns the creator model

Available Scopes

Scope Description
pending() Status = pending
inProgress() Status = in_progress
completed() Status = completed
cancelled() Status = cancelled
overdue() Not completed + due_at in the past
highPriority() Priority = high
dueToday() due_at is today

Enums

The package provides two enums for type safety:

use CharlesLightjarvis\Todo\Enums\TodoStatusEnum;
use CharlesLightjarvis\Todo\Enums\TodoPriorityEnum;

// Status values
TodoStatusEnum::PENDING->value;      // 'pending'
TodoStatusEnum::IN_PROGRESS->value;  // 'in_progress'
TodoStatusEnum::COMPLETED->value;    // 'completed'
TodoStatusEnum::CANCELLED->value;    // 'cancelled'

// Priority values
TodoPriorityEnum::LOW->value;    // 'low'
TodoPriorityEnum::MEDIUM->value; // 'medium'
TodoPriorityEnum::HIGH->value;   // 'high'

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Contributions are welcome! Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email charlestagne55@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

charleslightjarvis/laravel-todo 适用场景与选型建议

charleslightjarvis/laravel-todo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 04 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「task」 「laravel」 「todo」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 charleslightjarvis/laravel-todo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 charleslightjarvis/laravel-todo 我们能提供哪些服务?
定制开发 / 二次开发

基于 charleslightjarvis/laravel-todo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 35
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-04-23