### Install and Setup Laravel Forum Package Source: https://context7.com/team-tea-time/laravel-forum/llms.txt Commands to install the Laravel Forum package via Composer, publish its configuration and migration files, run database migrations, and install a UI preset. Optionally, it includes seeding sample data. ```bash composer require riari/laravel-forum:^7.0 php artisan vendor:publish --provider="TeamTeaTime\Forum\ForumServiceProvider" php artisan migrate php artisan forum:preset-install livewire-tailwind php artisan forum:seed ``` -------------------------------- ### Install UI Preset Source: https://github.com/team-tea-time/laravel-forum/blob/7.x/readme.md Installs a user interface preset for the forum, which publishes the corresponding views to the application. The 'livewire-tailwind' preset is the default and requires Livewire and other dependencies. ```bash php artisan forum:preset-install livewire-tailwind ``` -------------------------------- ### Run Local Development Tests Source: https://github.com/team-tea-time/laravel-forum/blob/7.x/readme.md Sets up and runs tests for local development of the Laravel Forum package. This involves starting the MySQL service, installing Composer dependencies, and executing PHPUnit tests. ```bash docker-compose up -d mysql docker-compose run --rm composer install docker-compose run --rm phpunit ``` -------------------------------- ### Install Laravel Forum Package Source: https://github.com/team-tea-time/laravel-forum/blob/7.x/readme.md Installs the Laravel Forum package version 6.0 or higher using Composer. This is the primary step for integrating the forum into a Laravel application. ```bash composer require riari/laravel-forum:^6.0 ``` -------------------------------- ### List Available UI Presets Source: https://github.com/team-tea-time/laravel-forum/blob/7.x/readme.md Lists all available UI presets that can be installed for the Laravel Forum package. This command helps users choose a frontend theme for the forum. ```bash php artisan forum:preset-list ``` -------------------------------- ### Publish Package Files Source: https://github.com/team-tea-time/laravel-forum/blob/7.x/readme.md Publishes the package's configuration files, translation files, and migration files to the application's directories. This allows for customization and database setup. ```bash php artisan vendor:publish ``` -------------------------------- ### Working with Forum Models (PHP) Source: https://context7.com/team-tea-time/laravel-forum/llms.txt Demonstrates how to use Laravel Forum's Eloquent models (Category, Thread, Post) for data retrieval and manipulation. Includes examples of querying, relationships, and model-specific scopes. ```php use TeamTeaTime\Forum\Models\Category; use TeamTeaTime\Forum\Models\Thread; use TeamTeaTime\Forum\Models\Post; use TeamTeaTime\Forum\Actions\CreateThread; use TeamTeaTime\Forum\Actions\CreatePost; use TeamTeaTime\Forum\Actions\CreateCategory; // Get all top-level categories $categories = Category::topLevel()->defaultOrder()->get(); // Get categories that accept threads $threadableCategories = Category::acceptsThreads()->get(); // Get category with its threads $category = Category::with(['threads' => function ($query) { $query->ordered()->withPostAndAuthorRelationships(); }])->find(1); // Create a category using the Action class $action = new CreateCategory( title: 'New Category', description: 'Description of the new category', colorLightMode: '#007bff', colorDarkMode: '#0d6efd', acceptsThreads: true, isPrivate: false, threadApprovalEnabled: false, postApprovalEnabled: false ); $category = $action->execute(); // Create a thread with initial post $action = new CreateThread( category: $category, author: auth()->user(), title: 'My New Thread', content: 'This is the content of my first post in this thread.' ); $thread = $action->execute(); // Create a reply post $action = new CreatePost( thread: $thread, parent: null, // or specify a Post for nested replies author: auth()->user(), content: 'This is a reply to the thread.' ); $post = $action->execute(); // Get recent threads $recentThreads = Thread::recent() ->withPostAndAuthorRelationships() ->get(); // Get ordered threads in a category $threads = Thread::where('category_id', $category->id) ->ordered() // Pinned first, then by updated_at desc ->approved() ->paginate(20); // Check if thread is accessible to user if ($thread->isAccessibleTo(auth()->user())) { // User can view this thread } // Mark thread as read for current user $thread->markAsRead(auth()->user()); // Get posts with pagination info for linking $post = Post::find(152); $pageNumber = $post->getPage(); // Returns the page number where this post appears ``` -------------------------------- ### API: List All Forum Categories Source: https://context7.com/team-tea-time/laravel-forum/llms.txt Bash command to retrieve all forum categories via the API. It supports filtering by `parent_id` to get descendants of a specific category and requires an authorization token. ```bash # Get all categories curl -X GET "https://example.com/forum/api/category" \ -H "Authorization: Bearer {token}" \ -H "Accept: application/json" # Response { "data": [ { "id": 1, "title": "General Discussion", "description": "General topics and announcements", "accepts_threads": true, "newest_thread_id": 42, "latest_active_thread_id": 40, "thread_count": 156, "post_count": 1024, "is_private": false, "parent_id": null, "color_light_mode": "#007bff", "color_dark_mode": "#0d6efd", "created_at": "2024-01-15T10:00:00.000000Z", "updated_at": "2024-03-20T14:30:00.000000Z", "actions": { "patch:update": "/forum/api/category/1", "delete:delete": "/forum/api/category/1" } } ], "links": { "self": "/forum/api/category/1", "newest_thread": "/forum/api/thread/42", "latest_active_thread": "/forum/api/thread/40" } } # Get descendants of a specific category curl -X GET "https://example.com/forum/api/category?parent_id=1" \ -H "Authorization: Bearer {token}" ``` -------------------------------- ### Get Recent Threads (API) Source: https://context7.com/team-tea-time/laravel-forum/llms.txt Retrieves a list of the most recent forum threads using a GET request to the API. Requires an authorization token. ```bash curl -X GET "https://example.com/forum/api/thread/recent" \ -H "Authorization: Bearer {token}" ``` -------------------------------- ### Get Unread Threads (API) Source: https://context7.com/team-tea-time/laravel-forum/llms.txt Fetches threads that have new content since the user's last read activity. Uses a GET request and requires an authorization token. ```bash curl -X GET "https://example.com/forum/api/thread/unread" \ -H "Authorization: Bearer {token}" ``` -------------------------------- ### Get Threads by Category Source: https://context7.com/team-tea-time/laravel-forum/llms.txt Retrieves a paginated list of threads within a specific category. Supports optional query parameters for filtering threads by creation and update dates. Requires authentication. ```bash # Get all threads in a category curl -X GET "https://example.com/forum/api/category/1/thread" \ -H "Authorization: Bearer {token}" \ -H "Accept: application/json" # Filter by date range curl -X GET "https://example.com/forum/api/category/1/thread?created_after=2024-01-01&updated_after=2024-03-01" \ -H "Authorization: Bearer {token}" ``` -------------------------------- ### API: Get Posts by Thread Source: https://context7.com/team-tea-time/laravel-forum/llms.txt Retrieve paginated posts for a specific thread. The response includes post details such as author information, content, sequence number, and timestamps. Requires authentication. ```bash curl -X GET "https://example.com/forum/api/thread/42/posts" \ -H "Authorization: Bearer {token}" \ -H "Accept: application/json" ``` -------------------------------- ### Get Recent Threads Source: https://context7.com/team-tea-time/laravel-forum/llms.txt Retrieves a list of the most recent threads in the forum. ```APIDOC ## GET /forum/api/thread/recent ### Description Retrieves a list of the most recent threads in the forum. ### Method GET ### Endpoint /forum/api/thread/recent ### Parameters #### Query Parameters None #### Request Body None ### Response #### Success Response (200) - **threads** (array) - An array of recent thread objects. #### Response Example { "threads": [ { "id": 1, "title": "Example Thread", "created_at": "2023-10-27T10:00:00Z", "updated_at": "2023-10-27T11:00:00Z" } ] } ``` -------------------------------- ### Get Unread Threads Source: https://context7.com/team-tea-time/laravel-forum/llms.txt Retrieves a list of threads that have new content since the user's last read. ```APIDOC ## GET /forum/api/thread/unread ### Description Retrieves a list of threads that have new content since the user's last read. ### Method GET ### Endpoint /forum/api/thread/unread ### Parameters #### Query Parameters None #### Request Body None ### Response #### Success Response (200) - **threads** (array) - An array of unread thread objects. #### Response Example { "threads": [ { "id": 2, "title": "Unread Thread", "created_at": "2023-10-27T10:00:00Z", "updated_at": "2023-10-27T11:00:00Z" } ] } ``` -------------------------------- ### GET /forum/api/category/{id}/thread Source: https://context7.com/team-tea-time/laravel-forum/llms.txt Retrieves a paginated list of threads within a specific category. Supports filtering by creation and update dates. ```APIDOC ## GET /forum/api/category/{id}/thread ### Description Retrieve paginated threads for a specific category with optional date filtering. Supports `created_after`, `created_before`, `updated_after`, and `updated_before` query parameters. ### Method GET ### Endpoint /forum/api/category/{id}/thread ### Parameters #### Path Parameters - **id** (integer) - Required - The ID of the category. #### Query Parameters - **created_after** (string) - Optional - Filter threads created after this date (YYYY-MM-DD). - **created_before** (string) - Optional - Filter threads created before this date (YYYY-MM-DD). - **updated_after** (string) - Optional - Filter threads updated after this date (YYYY-MM-DD). - **updated_before** (string) - Optional - Filter threads updated before this date (YYYY-MM-DD). ### Request Example ```bash # Get all threads in a category curl -X GET "https://example.com/forum/api/category/1/thread" \ -H "Authorization: Bearer {token}" \ -H "Accept: application/json" # Filter by date range curl -X GET "https://example.com/forum/api/category/1/thread?created_after=2024-01-01&updated_after=2024-03-01" \ -H "Authorization: Bearer {token}" ``` ### Response #### Success Response (200) - **data** (array) - An array of thread objects. - **id** (integer) - The unique identifier for the thread. - **category_id** (integer) - The ID of the category the thread belongs to. - **author_id** (integer) - The ID of the thread author. - **author_name** (string) - The name of the thread author. - **title** (string) - The title of the thread. - **pinned** (boolean) - Whether the thread is pinned. - **locked** (boolean) - Whether the thread is locked. - **first_post_id** (integer) - The ID of the first post in the thread. - **last_post_id** (integer) - The ID of the last post in the thread. - **reply_count** (integer) - The number of replies to the thread. - **created_at** (string) - Timestamp of creation. - **updated_at** (string) - Timestamp of last update. - **deleted_at** (string|null) - Timestamp of deletion, or null. - **actions** (object) - Available actions for the thread (e.g., lock, pin, delete). - **links** (object) - Pagination links (first, last, next, prev). - **meta** (object) - Pagination metadata (current_page, per_page, total). #### Response Example ```json { "data": [ { "id": 42, "category_id": 1, "author_id": 5, "author_name": "John Doe", "title": "Welcome to the forum!", "pinned": true, "locked": false, "first_post_id": 100, "last_post_id": 150, "reply_count": 50, "created_at": "2024-01-15T10:00:00.000000Z", "updated_at": "2024-03-20T14:30:00.000000Z", "deleted_at": null, "actions": { "post:lock": "/forum/api/thread/42/lock", "post:unlock": "/forum/api/thread/42/unlock", "post:pin": "/forum/api/thread/42/pin", "post:unpin": "/forum/api/thread/42/unpin", "post:rename": "/forum/api/thread/42/rename", "post:move": "/forum/api/thread/42/move", "delete:delete": "/forum/api/thread/42", "post:restore": "/forum/api/thread/42/restore" } } ], "links": { "first": "/forum/api/category/1/thread?page=1", "last": "/forum/api/category/1/thread?page=8", "next": "/forum/api/category/1/thread?page=2" }, "meta": { "current_page": 1, "per_page": 20, "total": 156 } } ``` ``` -------------------------------- ### API: List Categories Source: https://context7.com/team-tea-time/laravel-forum/llms.txt Retrieve all forum categories as a hierarchical tree filtered by user access permissions. Supports optional `parent_id` query parameter to get descendants of a specific category. ```APIDOC ## GET /forum/api/category ### Description Retrieve all forum categories as a hierarchical tree filtered by user access permissions. Supports optional `parent_id` query parameter to get descendants of a specific category. ### Method GET ### Endpoint /forum/api/category ### Parameters #### Query Parameters - **parent_id** (integer) - Optional - The ID of the parent category to retrieve descendants from. ### Request Example ```bash # Get all categories curl -X GET "https://example.com/forum/api/category" \ -H "Authorization: Bearer {token}" \ -H "Accept: application/json" # Get descendants of a specific category curl -X GET "https://example.com/forum/api/category?parent_id=1" \ -H "Authorization: Bearer {token}" ``` ### Response #### Success Response (200) - **data** (array) - An array of category objects. - **id** (integer) - The unique identifier for the category. - **title** (string) - The title of the category. - **description** (string) - A brief description of the category. - **accepts_threads** (boolean) - Indicates if new threads can be created in this category. - **newest_thread_id** (integer) - The ID of the newest thread in the category. - **latest_active_thread_id** (integer) - The ID of the most recently active thread. - **thread_count** (integer) - The total number of threads in the category. - **post_count** (integer) - The total number of posts in the category. - **is_private** (boolean) - Indicates if the category is private. - **parent_id** (integer|null) - The ID of the parent category, or null if it's a top-level category. - **color_light_mode** (string) - The color associated with the category in light mode. - **color_dark_mode** (string) - The color associated with the category in dark mode. - **created_at** (string) - The timestamp when the category was created. - **updated_at** (string) - The timestamp when the category was last updated. - **actions** (object) - Available actions for the category. - **patch:update** (string) - URL to update the category. - **delete:delete** (string) - URL to delete the category. - **links** (object) - Links related to the category. - **self** (string) - URL to the category resource. - **newest_thread** (string) - URL to the newest thread in the category. - **latest_active_thread** (string) - URL to the latest active thread in the category. #### Response Example ```json { "data": [ { "id": 1, "title": "General Discussion", "description": "General topics and announcements", "accepts_threads": true, "newest_thread_id": 42, "latest_active_thread_id": 40, "thread_count": 156, "post_count": 1024, "is_private": false, "parent_id": null, "color_light_mode": "#007bff", "color_dark_mode": "#0d6efd", "created_at": "2024-01-15T10:00:00.000000Z", "updated_at": "2024-03-20T14:30:00.000000Z", "actions": { "patch:update": "/forum/api/category/1", "delete:delete": "/forum/api/category/1" } } ], "links": { "self": "/forum/api/category/1", "newest_thread": "/forum/api/thread/42", "latest_active_thread": "/forum/api/thread/40" } } ``` ``` -------------------------------- ### Run Database Migrations Source: https://github.com/team-tea-time/laravel-forum/blob/7.x/readme.md Executes the database migrations published by the Laravel Forum package. This creates the necessary tables in the application's database for the forum functionality. ```bash php artisan migrate ``` -------------------------------- ### Create Forum Category Source: https://context7.com/team-tea-time/laravel-forum/llms.txt Creates a new forum category with specified details including title, description, colors, and optional settings for thread acceptance, privacy, and content approval. Requires authentication. ```bash curl -X POST "https://example.com/forum/api/category" \ -H "Authorization: Bearer {token}" \ -H "Content-Type: application/json" \ -H "Accept: application/json" \ -d '{ "title": "Development", "description": "Technical discussions about software development", "color_light_mode": "#28a745", "color_dark_mode": "#198754", "accepts_threads": true, "is_private": false, "thread_approval_enabled": false, "post_approval_enabled": false }' ``` -------------------------------- ### API: Create Post (Reply to Thread) Source: https://context7.com/team-tea-time/laravel-forum/llms.txt Create a new post as a reply to a thread. Supports nested replies by specifying a parent post ID. Content approval is subject to category settings. Requires authentication. ```bash curl -X POST "https://example.com/forum/api/thread/42/posts" \ -H "Authorization: Bearer {token}" \ -H "Content-Type: application/json" \ -H "Accept: application/json" \ -d '{ "content": "Great question! Here is how you can configure the API authentication...", "post_id": 100 }' ``` -------------------------------- ### Configure Forum General Settings Source: https://context7.com/team-tea-time/laravel-forum/llms.txt PHP configuration for general forum settings, including thresholds for old threads, soft delete behavior, display of trashed posts, pagination limits for threads and posts, and validation rules for thread titles and content. ```php // config/forum/general.php return [ 'old_thread_threshold' => '7 days', // Threads older than this are considered "old" 'soft_deletes' => true, 'display_trashed_posts' => true, 'pagination' => [ 'threads' => 20, 'posts' => 20, ], 'validation' => [ 'title_min' => 3, 'content_min' => 3, ], 'content_approval' => [ 'threads' => ['enable_globally' => false, 'rejection_should_soft_delete' => false], 'posts' => ['enable_globally' => false, 'rejection_should_soft_delete' => false], ], ]; ``` -------------------------------- ### Integrating with Laravel Forum Events Source: https://context7.com/team-tea-time/laravel-forum/llms.txt Listen to forum events to trigger notifications, logging, or additional business logic. This involves registering listeners in the `EventServiceProvider` and creating listener classes. ```php // Available events in TeamTeaTime\Forum\Events namespace use TeamTeaTime\Forum\Events\UserCreatedThread; use TeamTeaTime\Forum\Events\UserCreatedPost; use TeamTeaTime\Forum\Events\UserLockedThread; use TeamTeaTime\Forum\Events\UserApprovedThread; use TeamTeaTime\Forum\Events\UserBulkMovedThreads; // Register listeners in EventServiceProvider protected $listen = [ \TeamTeaTime\Forum\Events\UserCreatedThread::class => [ \App\Listeners\SendNewThreadNotification::class, ], \TeamTeaTime\Forum\Events\UserCreatedPost::class => [ \App\Listeners\NotifyThreadParticipants::class, ], ]; // Example listener namespace App\Listeners; use TeamTeaTime\Forum\Events\UserCreatedPost; class NotifyThreadParticipants { public function handle(UserCreatedPost $event) { $post = $event->post; $thread = $post->thread; // Get all participants except the author $participants = $thread->posts() ->distinct('author_id') ->where('author_id', '!=', $post->author_id) ->pluck('author_id'); // Send notifications... } } ``` -------------------------------- ### Configure Forum Integration Settings Source: https://context7.com/team-tea-time/laravel-forum/llms.txt PHP configuration for forum integration, defining policies for categories, threads, and posts, specifying the user model, and setting the attribute for the user's display name. ```php // config/forum/integration.php return [ 'policies' => [ 'forum' => TeamTeaTime\Forum\Policies\ForumPolicy::class, 'model' => [ TeamTeaTime\Forum\Models\Category::class => TeamTeaTime\Forum\Policies\CategoryPolicy::class, TeamTeaTime\Forum\Models\Thread::class => TeamTeaTime\Forum\Policies\ThreadPolicy::class, TeamTeaTime\Forum\Models\Post::class => TeamTeaTime\Forum\Policies\PostPolicy::class, ], ], 'user_model' => App\Models\User::class, 'user_name' => 'name', // Attribute on user model for display name ]; ``` -------------------------------- ### Configure Forum API Settings Source: https://context7.com/team-tea-time/laravel-forum/llms.txt PHP configuration for the forum's RESTful API, enabling the API and search functionality, and defining routing, authentication middleware, and naming conventions for API endpoints. ```php // config/forum/api.php return [ 'enable' => true, 'enable_search' => true, 'router' => [ 'prefix' => '/forum/api', 'as' => 'forum.api.', 'middleware' => ['api', 'auth:api'], 'auth_middleware' => ['auth:api'], ], ]; ``` -------------------------------- ### Create New Forum Thread Source: https://context7.com/team-tea-time/laravel-forum/llms.txt Creates a new discussion thread within a specified category. The thread is initiated with an initial post containing the provided content. Content approval is subject to category settings. Requires authentication. ```bash curl -X POST "https://example.com/forum/api/category/1/thread" \ -H "Authorization: Bearer {token}" \ -H "Content-Type: application/json" \ -H "Accept: application/json" \ -d '{ "title": "How to configure the API?", "content": "I am trying to set up the API endpoints for my application. Can someone help me understand the authentication requirements and best practices for securing the endpoints?" }' ``` -------------------------------- ### Frontend Helpers and Routes in Laravel Forum Source: https://context7.com/team-tea-time/laravel-forum/llms.txt Utilize the Forum utility class in Blade views for generating routes and rendering content safely. It provides functions for creating model-specific routes and safely rendering post content. ```php // In Blade views, Forum is aliased automatically use TeamTeaTime\Forum\Support\Frontend\Forum; // Generate routes for models $categoryUrl = Forum::route('category.show', $category); $threadUrl = Forum::route('thread.show', $thread); $postUrl = Forum::route('thread.show', $post); // Includes page number and anchor // Render post content safely (escapes HTML and converts newlines) $renderedContent = Forum::render($post->content); // Show flash alerts Forum::alert('success', 'general.thread_created', 1, ['title' => $thread->title]); // In Blade templates {{ $category->title }}