PHP code example of charleslightjarvis / laravel-todo
1. Go to this page and download the library: Download charleslightjarvis/laravel-todo library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
charleslightjarvis / laravel-todo example snippets
use CharlesLightjarvis\Todo\Traits\HasTodos;
class User extends Model
{
use HasTodos;
}
class Project extends Model
{
use 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);
$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();
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();
// 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
// All todos attached to the model
$user->todos;
// All todos created by the model (via the creator relation)
$user->createdTodos;
// 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