Installation:
composer require moox/item
php artisan moox:install
moox:install command sets up migrations, models, and basic configurations for the Item entity.First Use Case:
use Moox\Item\Models\Item;
$item = Item::create([
'title' => 'My First Item',
'slug' => 'my-first-item',
'content' => '# Hello World',
'type' => 'log', // or any custom type
]);
$item = Item::find($item->id);
echo $item->title; // Outputs: "My First Item"
Where to Look First:
database/migrations/ for the items table structure.app/Models/Moox/Item.php for available methods and relationships.CRUD Operations:
Item::create() with an associative array of fields (e.g., title, content, type).Item::find($id)->update([...]) or $item->update([...]).$item->delete() or Item::destroy($id).Relationships:
$item = Item::with('author')->find($id);
$item->author->name; // Access user details
$item->taxonomies()->attach(['category_id' => 1, 'tag_id' => 2]);
$item->taxonomies; // Collection of taxonomy models
$item->relations()->attach(['related_item_id' => 3]);
Content Management:
$item->content = "# Heading\n\nThis is **markdown**.";
$item->save();
parsedown to convert markdown to HTML:
use Parsedown;
$parsedown = new Parsedown();
echo $parsedown->text($item->content);
Data Key-Value Pairs:
$item->data = ['key1' => 'value1', 'key2' => 'value2'];
$item->save();
// Access later:
$item->data->key1; // Outputs: "value1"
Active Toggle:
$item->active = false; // Disable item
$item->save();
// Query active items:
Item::where('active', true)->get();
Media (Image):
$item->image()->attach($mediaId); // Attach existing media
// Or upload directly (if using a media library like Spatie Media Library):
$item->addMediaFromRequest('image')->toMediaCollection('images');
Sections:
$item->section = 'news'; // Assign to a section
$item->save();
// Query items by section:
Item::where('section', 'news')->get();
Logging System:
log type item for system logs:
Item::create([
'title' => 'Login Attempt',
'type' => 'log',
'content' => 'User `john` logged in at ' . now(),
'data' => ['ip' => request()->ip(), 'user_agent' => request()->userAgent()],
]);
Content Management System (CMS):
Item as a flexible CMS entry with content (markdown), type (e.g., article, page), and taxonomies (categories/tags).$article = Item::create([
'title' => 'Laravel 10 Features',
'slug' => 'laravel-10-features',
'content' => '# Laravel 10\n\nNew features...',
'type' => 'article',
'section' => 'blog',
]);
$article->taxonomies()->attach(['category_id' => 1]); // Attach category
Task Management:
due (datetime), type (e.g., task), and color for visual distinction:
$task = Item::create([
'title' => 'Finish Project',
'type' => 'task',
'due' => now()->addDays(7),
'color' => '#FF5733',
]);
Laravel Scout:
title, content, etc.:
use Moox\Item\Models\Item;
use Laravel\Scout\Searchable;
class Item extends Model implements Searchable
{
public function toSearchableArray()
{
return [
'title' => $this->title,
'content' => $this->content,
'type' => $this->type,
];
}
}
php artisan scout:import "App\Models\Moox\Item"
API Resources:
php artisan make:resource ItemResource
// app/Http/Resources/ItemResource.php
public function toArray($request)
{
return [
'id' => $this->id,
'title' => $this->title,
'slug' => $this->slug,
'content' => $this->content,
'type' => $this->type,
'due' => $this->due,
'author' => AuthorResource::make($this->author),
];
}
Form Requests:
php artisan make:request StoreItemRequest
// app/Http/Requests/StoreItemRequest.php
public function rules()
{
return [
'title' => 'required|string|max:255',
'content' => 'nullable|string',
'type' => 'required|string',
'due' => 'nullable|date',
];
}
Events:
// app/Providers/EventServiceProvider.php
protected $listen = [
'Moox\Item\Events\ItemCreated' => [
'App\Listeners\LogItemCreation',
],
];
Policies:
php artisan make:policy ItemPolicy --model=Item
// app/Policies/ItemPolicy.php
public function update(User $user, Item $item)
{
return $user->id === $item->author_id;
}
UUID/ULID Conflicts:
Item model uses both uuid and ulid as identifiers. Ensure your database supports these types (e.g., char(36) for UUID, binary(16) for ULID).Item::where('uuid', $uuid)->first();
// or
Item::where('ulid', $ulid)->first();
Media Handling:
image or other media fields, ensure the spatie/laravel-medialibrary package is installed and configured. The moox:install command may not cover this automatically.composer require spatie/laravel-medialibrary
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider"
Taxonomy Relationships:
item_taxonomies) exists and is properly linked. Run migrations if needed:
php artisan migrate
Slug Generation:
slug in the create or update array. Conflicts may arise if slugs are not unique:
$item = Item::create([
'title' => 'New Post',
'slug' => 'custom-slug', // Override auto-generated slug
]);
Active Toggle:
How can I help you explore Laravel packages today?