mirror of
https://github.com/openclassify/openclassify.git
synced 2026-04-16 20:22:10 -05:00
Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
239fd0d2bf | ||
|
|
f06943ce9d | ||
|
|
057620b715 | ||
|
|
6b3a8b8581 | ||
|
|
d2345cbeda | ||
|
|
6ea371e372 | ||
|
|
f8c953d37c | ||
|
|
3e413e2fed | ||
|
|
de09a50893 | ||
|
|
e601c3dd9f | ||
|
|
5ce2b15d3d | ||
|
|
4f0b6d0ef2 | ||
|
|
4e0ed5ca36 | ||
|
|
8c0365e710 | ||
|
|
46b70a91f7 | ||
|
|
6fde32cc8b | ||
|
|
222928d1d9 | ||
|
|
89003f45e1 | ||
|
|
f1e2199fef | ||
|
|
08aad25594 | ||
|
|
33570c18d3 | ||
|
|
0d53e59dbd | ||
|
|
e79fd20801 |
413
.agents/skills/developing-with-ai-sdk/SKILL.md
Normal file
413
.agents/skills/developing-with-ai-sdk/SKILL.md
Normal file
@ -0,0 +1,413 @@
|
||||
---
|
||||
name: developing-with-ai-sdk
|
||||
description: Builds AI agents, generates text and chat responses, produces images, synthesizes audio, transcribes speech, generates vector embeddings, reranks documents, and manages files and vector stores using the Laravel AI SDK (laravel/ai). Supports structured output, streaming, tools, conversation memory, middleware, queueing, broadcasting, and provider failover. Use when building, editing, updating, debugging, or testing any AI functionality, including agents, LLMs, chatbots, text generation, image generation, audio, transcription, embeddings, RAG, similarity search, vector stores, prompting, structured output, or any AI provider (OpenAI, Anthropic, Gemini, Cohere, Groq, xAI, ElevenLabs, Jina, OpenRouter).
|
||||
---
|
||||
|
||||
# Developing with the Laravel AI SDK
|
||||
|
||||
The Laravel AI SDK (`laravel/ai`) is the official AI package for Laravel, providing a unified API for agents, images, audio, transcription, embeddings, reranking, vector stores, and file management across multiple AI providers.
|
||||
|
||||
## Searching the Documentation
|
||||
|
||||
This package is new. Always search the documentation before implementing any feature. Never guess at APIs — the documentation is the single source of truth.
|
||||
|
||||
- Use broad, simple queries that match the documentation section headings below.
|
||||
- Do not add package names to queries — package information is shared automatically. Use `test agent fake`, not `laravel ai test agent fake`.
|
||||
- Run multiple queries at once — the most relevant results are returned first.
|
||||
|
||||
### Documentation Sections
|
||||
|
||||
Use these section headings as query terms for accurate results:
|
||||
|
||||
- Introduction, Installation, Configuration, Provider Support
|
||||
- Agents: Prompting, Conversation Context, Structured Output, Attachments, Streaming, Broadcasting, Queueing, Tools, Provider Tools, Middleware, Anonymous Agents, Agent Configuration
|
||||
- Images
|
||||
- Audio (TTS)
|
||||
- Transcription (STT)
|
||||
- Embeddings: Querying Embeddings, Caching Embeddings
|
||||
- Reranking
|
||||
- Files
|
||||
- Vector Stores: Adding Files to Stores
|
||||
- Failover
|
||||
- Testing: Agents, Images, Audio, Transcriptions, Embeddings, Reranking, Files, Vector Stores
|
||||
- Events
|
||||
|
||||
## Decision Workflow
|
||||
|
||||
Determine the right entry point before writing code:
|
||||
|
||||
Text generation or chat? → Agent class with `Promptable` trait
|
||||
Chat with conversation history? → Agent + `Conversational` interface (manual) or `RemembersConversations` trait (automatic)
|
||||
Structured JSON output? → Agent + `HasStructuredOutput` interface
|
||||
Image generation? → `Image::of()->generate()`
|
||||
Audio synthesis? → `Audio::of()->generate()`
|
||||
Transcription? → `Transcription::fromPath()->generate()`
|
||||
Embeddings? → `Embeddings::for()->generate()`
|
||||
Reranking? → `Reranking::of()->rerank()`
|
||||
File storage? → `Document::fromPath()->put()`
|
||||
Vector stores? → `Stores::create()`
|
||||
|
||||
## Basic Usage Examples
|
||||
|
||||
### Agents
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Contracts\Agent;
|
||||
use Laravel\Ai\Promptable;
|
||||
|
||||
class SalesCoach implements Agent
|
||||
{
|
||||
use Promptable;
|
||||
|
||||
public function instructions(): string
|
||||
{
|
||||
return 'You are a sales coach.';
|
||||
}
|
||||
}
|
||||
|
||||
// Prompting
|
||||
$response = (new SalesCoach)->prompt('Analyze this transcript...');
|
||||
echo $response->text;
|
||||
|
||||
// Streaming (returns SSE response from a route)
|
||||
return (new SalesCoach)->stream('Analyze this transcript...');
|
||||
|
||||
// Queueing
|
||||
(new SalesCoach)->queue('Analyze this transcript...')
|
||||
->then(fn ($response) => /* ... */);
|
||||
|
||||
// Anonymous agents
|
||||
use function Laravel\Ai\{agent};
|
||||
|
||||
$response = agent(instructions: 'You are a helpful assistant.')->prompt('Hello');
|
||||
```
|
||||
|
||||
### Conversation Context
|
||||
|
||||
Manual conversation history via the `Conversational` interface:
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Contracts\Agent;
|
||||
use Laravel\Ai\Contracts\Conversational;
|
||||
use Laravel\Ai\Messages\Message;
|
||||
use Laravel\Ai\Promptable;
|
||||
|
||||
class SalesCoach implements Agent, Conversational
|
||||
{
|
||||
use Promptable;
|
||||
|
||||
public function __construct(public User $user) {}
|
||||
|
||||
public function instructions(): string { return 'You are a sales coach.'; }
|
||||
|
||||
public function messages(): iterable
|
||||
{
|
||||
return History::where('user_id', $this->user->id)
|
||||
->latest()->limit(50)->get()->reverse()
|
||||
->map(fn ($m) => new Message($m->role, $m->content))
|
||||
->all();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Automatic conversation persistence via the `RemembersConversations` trait:
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Concerns\RemembersConversations;
|
||||
use Laravel\Ai\Contracts\Agent;
|
||||
use Laravel\Ai\Contracts\Conversational;
|
||||
use Laravel\Ai\Promptable;
|
||||
|
||||
class SalesCoach implements Agent, Conversational
|
||||
{
|
||||
use Promptable, RemembersConversations;
|
||||
|
||||
public function instructions(): string { return 'You are a sales coach.'; }
|
||||
}
|
||||
|
||||
// Start a new conversation
|
||||
$response = (new SalesCoach)->forUser($user)->prompt('Hello!');
|
||||
$conversationId = $response->conversationId;
|
||||
|
||||
// Continue an existing conversation
|
||||
$response = (new SalesCoach)->continue($conversationId, as: $user)->prompt('Tell me more.');
|
||||
```
|
||||
|
||||
### Structured Output
|
||||
|
||||
```php
|
||||
use Illuminate\Contracts\JsonSchema\JsonSchema;
|
||||
use Laravel\Ai\Contracts\Agent;
|
||||
use Laravel\Ai\Contracts\HasStructuredOutput;
|
||||
use Laravel\Ai\Promptable;
|
||||
|
||||
class Reviewer implements Agent, HasStructuredOutput
|
||||
{
|
||||
use Promptable;
|
||||
|
||||
public function instructions(): string { return 'Review and score content.'; }
|
||||
|
||||
public function schema(JsonSchema $schema): array
|
||||
{
|
||||
return [
|
||||
'feedback' => $schema->string()->required(),
|
||||
'score' => $schema->integer()->min(1)->max(10)->required(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$response = (new Reviewer)->prompt('Review this...');
|
||||
echo $response['score']; // Access like an array
|
||||
```
|
||||
|
||||
### Images
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Image;
|
||||
|
||||
$image = Image::of('A sunset over mountains')
|
||||
->landscape()
|
||||
->quality('high')
|
||||
->generate();
|
||||
|
||||
$path = $image->store(); // Store to default disk
|
||||
```
|
||||
|
||||
### Audio
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Audio;
|
||||
|
||||
$audio = Audio::of('Hello from Laravel.')
|
||||
->female()
|
||||
->instructions('Speak warmly')
|
||||
->generate();
|
||||
|
||||
$path = $audio->store();
|
||||
```
|
||||
|
||||
### Transcription
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Transcription;
|
||||
|
||||
$transcript = Transcription::fromStorage('audio.mp3')
|
||||
->diarize()
|
||||
->generate();
|
||||
|
||||
echo (string) $transcript;
|
||||
```
|
||||
|
||||
### Embeddings
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Embeddings;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
$response = Embeddings::for(['Text one', 'Text two'])
|
||||
->dimensions(1536)
|
||||
->cache()
|
||||
->generate();
|
||||
|
||||
// Single string via Stringable
|
||||
$embedding = Str::of('Napa Valley has great wine.')->toEmbeddings();
|
||||
```
|
||||
|
||||
### Reranking
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Reranking;
|
||||
|
||||
$response = Reranking::of(['Django is Python.', 'Laravel is PHP.', 'React is JS.'])
|
||||
->limit(5)
|
||||
->rerank('PHP frameworks');
|
||||
|
||||
$response->first()->document; // "Laravel is PHP."
|
||||
```
|
||||
|
||||
### Files and Vector Stores
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Files\Document;
|
||||
use Laravel\Ai\Stores;
|
||||
|
||||
// Store a file with the provider
|
||||
$file = Document::fromPath('/path/to/doc.pdf')->put();
|
||||
|
||||
// Create a vector store and add files
|
||||
$store = Stores::create('Knowledge Base');
|
||||
$store->add($file->id);
|
||||
$store->add(Document::fromStorage('manual.pdf')); // Store + add in one step
|
||||
```
|
||||
|
||||
## Agent Configuration
|
||||
|
||||
### PHP Attributes
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Attributes\{Provider, MaxSteps, MaxTokens, Temperature, Timeout};
|
||||
|
||||
#[Provider('anthropic')]
|
||||
#[MaxSteps(10)]
|
||||
#[MaxTokens(4096)]
|
||||
#[Temperature(0.7)]
|
||||
#[Timeout(120)]
|
||||
class MyAgent implements Agent
|
||||
{
|
||||
use Promptable;
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
The `#[UseCheapestModel]` and `#[UseSmartestModel]` attributes are also available for automatic model selection.
|
||||
|
||||
### Tools
|
||||
|
||||
Implement the `HasTools` interface and scaffold tools with `php artisan make:tool`:
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Contracts\HasTools;
|
||||
|
||||
class MyAgent implements Agent, HasTools
|
||||
{
|
||||
use Promptable;
|
||||
|
||||
public function tools(): iterable
|
||||
{
|
||||
return [new MyCustomTool];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Provider Tools
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Providers\Tools\{WebSearch, WebFetch, FileSearch};
|
||||
|
||||
public function tools(): iterable
|
||||
{
|
||||
return [
|
||||
(new WebSearch)->max(5)->allow(['laravel.com']),
|
||||
new WebFetch,
|
||||
new FileSearch(stores: ['store_id']),
|
||||
];
|
||||
}
|
||||
```
|
||||
|
||||
### Conversation Memory
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Concerns\RemembersConversations;
|
||||
use Laravel\Ai\Contracts\Conversational;
|
||||
|
||||
class ChatBot implements Agent, Conversational
|
||||
{
|
||||
use Promptable, RemembersConversations;
|
||||
// ...
|
||||
}
|
||||
|
||||
$response = (new ChatBot)->forUser($user)->prompt('Hello!');
|
||||
$response = (new ChatBot)->continue($conversationId, as: $user)->prompt('More...');
|
||||
```
|
||||
|
||||
### Failover
|
||||
|
||||
```php
|
||||
$response = (new MyAgent)->prompt('Hello', provider: ['openai', 'anthropic']);
|
||||
```
|
||||
|
||||
## Testing and Faking
|
||||
|
||||
Each capability supports `fake()` with assertions:
|
||||
|
||||
```php
|
||||
use App\Ai\Agents\SalesCoach;
|
||||
use Laravel\Ai\{Image, Audio, Transcription, Embeddings, Reranking, Files, Stores};
|
||||
|
||||
// Agents
|
||||
SalesCoach::fake(['Response 1', 'Response 2']);
|
||||
SalesCoach::assertPrompted('query');
|
||||
SalesCoach::assertNotPrompted('query');
|
||||
SalesCoach::assertNeverPrompted();
|
||||
SalesCoach::fake()->preventStrayPrompts();
|
||||
|
||||
// Images
|
||||
Image::fake();
|
||||
Image::assertGenerated(fn ($prompt) => $prompt->contains('sunset'));
|
||||
Image::assertNothingGenerated();
|
||||
|
||||
// Audio
|
||||
Audio::fake();
|
||||
Audio::assertGenerated(fn ($prompt) => $prompt->contains('Hello'));
|
||||
|
||||
// Transcription
|
||||
Transcription::fake(['Transcribed text.']);
|
||||
Transcription::assertGenerated(fn ($prompt) => $prompt->isDiarized());
|
||||
|
||||
// Embeddings
|
||||
Embeddings::fake();
|
||||
Embeddings::assertGenerated(fn ($prompt) => $prompt->contains('Laravel'));
|
||||
|
||||
// Reranking
|
||||
Reranking::fake();
|
||||
Reranking::assertReranked(fn ($prompt) => $prompt->contains('PHP'));
|
||||
|
||||
// Files
|
||||
Files::fake();
|
||||
Files::assertStored(fn ($file) => $file->mimeType() === 'text/plain');
|
||||
|
||||
// Stores
|
||||
Stores::fake();
|
||||
Stores::assertCreated('Knowledge Base');
|
||||
$store = Stores::get('id');
|
||||
$store->assertAdded('file_id');
|
||||
```
|
||||
|
||||
## Key Patterns
|
||||
|
||||
- Namespace: `Laravel\Ai\`
|
||||
- Package: `composer require laravel/ai`
|
||||
- Agent pattern: Implement the `Agent` interface and use the `Promptable` trait
|
||||
- Optional interfaces: `HasTools`, `HasMiddleware`, `HasStructuredOutput`, `Conversational`
|
||||
- Entry-point classes: `Image`, `Audio`, `Transcription`, `Embeddings`, `Reranking`, `Stores`
|
||||
- Artisan commands: `php artisan make:agent`, `php artisan make:tool`
|
||||
- Global helper: `agent()` for anonymous agents
|
||||
|
||||
## Common Pitfalls
|
||||
|
||||
### Wrong Namespace
|
||||
|
||||
The namespace is `Laravel\Ai`, not `Illuminate\Ai` or `Laravel\AI`.
|
||||
|
||||
```php
|
||||
// Correct
|
||||
use Laravel\Ai\Image;
|
||||
use Laravel\Ai\Contracts\Agent;
|
||||
use Laravel\Ai\Promptable;
|
||||
|
||||
// Wrong — these do not exist
|
||||
use Illuminate\Ai\Image;
|
||||
use Laravel\AI\Agent;
|
||||
```
|
||||
|
||||
### Unsupported Provider Capability
|
||||
|
||||
Calling a capability not supported by a provider throws a `LogicException`. Refer to the provider support table below.
|
||||
|
||||
### Never Use Prism Directly
|
||||
|
||||
Use agents and entry-point classes (`Image`, `Audio`, etc.) — not `Prism::text()` directly. The AI SDK wraps Prism internally.
|
||||
|
||||
## Provider Support
|
||||
|
||||
| Provider | Text | Image | Audio | STT | Embeddings | Reranking | Files | Stores |
|
||||
| ---------- | ---- | ----- | ----- | --- | ---------- | --------- | ----- | ------ |
|
||||
| OpenAI | Y | Y | Y | Y | Y | - | Y | Y |
|
||||
| Anthropic | Y | - | - | - | - | - | Y | - |
|
||||
| Gemini | Y | Y | - | - | Y | - | Y | Y |
|
||||
| xAI | Y | Y | - | - | - | - | - | - |
|
||||
| Groq | Y | - | - | - | - | - | - | - |
|
||||
| OpenRouter | Y | - | - | - | - | - | - | - |
|
||||
| ElevenLabs | - | - | Y | Y | - | - | - | - |
|
||||
| Cohere | - | - | - | - | Y | Y | - | - |
|
||||
| Jina | - | - | - | - | Y | Y | - | - |
|
||||
100
.agents/skills/tailwindcss-development/SKILL.md
Normal file
100
.agents/skills/tailwindcss-development/SKILL.md
Normal file
@ -0,0 +1,100 @@
|
||||
---
|
||||
name: tailwindcss-development
|
||||
description: "Styles applications using Tailwind CSS v3 utilities. Activates when adding styles, restyling components, working with gradients, spacing, layout, flex, grid, responsive design, dark mode, colors, typography, or borders; or when the user mentions CSS, styling, classes, Tailwind, restyle, hero section, cards, buttons, or any visual/UI changes."
|
||||
license: MIT
|
||||
metadata:
|
||||
author: laravel
|
||||
---
|
||||
|
||||
# Tailwind CSS Development
|
||||
|
||||
## When to Apply
|
||||
|
||||
Activate this skill when:
|
||||
- Adding styles to components or pages
|
||||
- Working with responsive design
|
||||
- Implementing dark mode
|
||||
- Extracting repeated patterns into components
|
||||
- Debugging spacing or layout issues
|
||||
|
||||
## Documentation
|
||||
|
||||
Use `search-docs` for detailed Tailwind CSS v3 patterns and documentation.
|
||||
|
||||
## Basic Usage
|
||||
|
||||
- Use Tailwind CSS classes to style HTML. Check and follow existing Tailwind conventions in the project before introducing new patterns.
|
||||
- Offer to extract repeated patterns into components that match the project's conventions (e.g., Blade, JSX, Vue).
|
||||
- Consider class placement, order, priority, and defaults. Remove redundant classes, add classes to parent or child elements carefully to reduce repetition, and group elements logically.
|
||||
|
||||
## Tailwind CSS v3 Specifics
|
||||
|
||||
- Always use Tailwind CSS v3 and verify you're using only classes it supports.
|
||||
- Configuration is done in the `tailwind.config.js` file.
|
||||
- Import using `@tailwind` directives:
|
||||
|
||||
<!-- v3 Import Syntax -->
|
||||
```css
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
```
|
||||
|
||||
## Spacing
|
||||
|
||||
When listing items, use gap utilities for spacing; don't use margins.
|
||||
|
||||
<!-- Gap Utilities -->
|
||||
```html
|
||||
<div class="flex gap-8">
|
||||
<div>Item 1</div>
|
||||
<div>Item 2</div>
|
||||
</div>
|
||||
```
|
||||
|
||||
## Dark Mode
|
||||
|
||||
If existing pages and components support dark mode, new pages and components must support it the same way, typically using the `dark:` variant:
|
||||
|
||||
<!-- Dark Mode -->
|
||||
```html
|
||||
<div class="bg-white dark:bg-gray-900 text-gray-900 dark:text-white">
|
||||
Content adapts to color scheme
|
||||
</div>
|
||||
```
|
||||
|
||||
## Common Patterns
|
||||
|
||||
### Flexbox Layout
|
||||
|
||||
<!-- Flexbox Layout -->
|
||||
```html
|
||||
<div class="flex items-center justify-between gap-4">
|
||||
<div>Left content</div>
|
||||
<div>Right content</div>
|
||||
</div>
|
||||
```
|
||||
|
||||
### Grid Layout
|
||||
|
||||
<!-- Grid Layout -->
|
||||
```html
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
<div>Card 1</div>
|
||||
<div>Card 2</div>
|
||||
<div>Card 3</div>
|
||||
</div>
|
||||
```
|
||||
|
||||
## Verification
|
||||
|
||||
1. Check browser for visual rendering
|
||||
2. Test responsive breakpoints
|
||||
3. Verify dark mode if project uses it
|
||||
|
||||
## Common Pitfalls
|
||||
|
||||
- Using margins for spacing between siblings instead of gap utilities
|
||||
- Forgetting to add dark mode variants when the project uses dark mode
|
||||
- Not checking existing project conventions before adding new utilities
|
||||
- Overusing inline styles when Tailwind classes would suffice
|
||||
@ -5,3 +5,4 @@ Act as a Senior Laravel & FilamentPHP Architect. Refactor the attached code as a
|
||||
4. Database: Consolidate migrations into a single file per table or topic (e.g., users, cache, jobs) to reduce the overall number of migration files.
|
||||
5. Modularity: Use the `laravel-modules` package to encapsulate all features, routing, and Filament resources strictly inside their respective modules.
|
||||
6. Frontend: Optimize and reduce the CSS footprint while maintaining the exact same visual output.
|
||||
7. Tooling: Use Laravel Boost as the default framework intelligence layer. Search Laravel Boost documentation before framework-level changes and prefer Boost MCP tools for artisan, browser logs, database inspection, and debugging.
|
||||
|
||||
22
.env.example
22
.env.example
@ -33,7 +33,7 @@ SESSION_ENCRYPT=false
|
||||
SESSION_PATH=/
|
||||
SESSION_DOMAIN=null
|
||||
|
||||
BROADCAST_CONNECTION=log
|
||||
BROADCAST_CONNECTION=reverb
|
||||
FILESYSTEM_DISK=s3
|
||||
MEDIA_DISK=s3
|
||||
LOCAL_MEDIA_DISK=public
|
||||
@ -64,6 +64,20 @@ AWS_URL=
|
||||
AWS_ENDPOINT=https://hel1.your-objectstorage.com
|
||||
AWS_USE_PATH_STYLE_ENDPOINT=false
|
||||
|
||||
REVERB_APP_ID=
|
||||
REVERB_APP_KEY=
|
||||
REVERB_APP_SECRET=
|
||||
REVERB_HOST=localhost
|
||||
REVERB_PORT=8080
|
||||
REVERB_SCHEME=http
|
||||
REVERB_SERVER_HOST=0.0.0.0
|
||||
REVERB_SERVER_PORT=8080
|
||||
|
||||
VITE_REVERB_APP_KEY="${REVERB_APP_KEY}"
|
||||
VITE_REVERB_HOST="${REVERB_HOST}"
|
||||
VITE_REVERB_PORT="${REVERB_PORT}"
|
||||
VITE_REVERB_SCHEME="${REVERB_SCHEME}"
|
||||
|
||||
VITE_APP_NAME="${APP_NAME}"
|
||||
|
||||
OPENAI_API_KEY=
|
||||
@ -73,4 +87,8 @@ QUICK_LISTING_AI_MODEL=gpt-5.2
|
||||
|
||||
|
||||
DEMO=0
|
||||
DEMO_TTL_MINUTES=360
|
||||
DEMO_TTL_MINUTES=360
|
||||
DEMO_TURNSTILE_ENABLED=0
|
||||
TURNSTILE_SITE_KEY=0x4AAAAAACogGCt62w6ahqM4
|
||||
TURNSTILE_SECRET_KEY=0x4AAAAAACogGLdg-1mydGAW8FT_He6DTI8
|
||||
TURNSTILE_TIMEOUT_SECONDS=8
|
||||
|
||||
@ -4,4 +4,5 @@ Act as a Senior Laravel & FilamentPHP Architect. Refactor the attached code as a
|
||||
3. Refactoring: Move all database logic into Models and extract repetitive Filament code into dedicated Helper classes. Identify and fix any existing logical errors.
|
||||
4. Database: Consolidate migrations into a single file per table or topic (e.g., users, cache, jobs) to reduce the overall number of migration files.
|
||||
5. Modularity: Use the `laravel-modules` package to encapsulate all features, routing, and Filament resources strictly inside their respective modules.
|
||||
6. Frontend: Optimize and reduce the CSS footprint while maintaining the exact same visual output.
|
||||
6. Frontend: Optimize and reduce the CSS footprint while maintaining the exact same visual output.
|
||||
7. Tooling: Use Laravel Boost as the default framework intelligence layer. Search Laravel Boost documentation before framework-level changes and prefer Boost MCP tools for artisan, browser logs, database inspection, and debugging.
|
||||
|
||||
413
.github/skills/developing-with-ai-sdk/SKILL.md
vendored
Normal file
413
.github/skills/developing-with-ai-sdk/SKILL.md
vendored
Normal file
@ -0,0 +1,413 @@
|
||||
---
|
||||
name: developing-with-ai-sdk
|
||||
description: Builds AI agents, generates text and chat responses, produces images, synthesizes audio, transcribes speech, generates vector embeddings, reranks documents, and manages files and vector stores using the Laravel AI SDK (laravel/ai). Supports structured output, streaming, tools, conversation memory, middleware, queueing, broadcasting, and provider failover. Use when building, editing, updating, debugging, or testing any AI functionality, including agents, LLMs, chatbots, text generation, image generation, audio, transcription, embeddings, RAG, similarity search, vector stores, prompting, structured output, or any AI provider (OpenAI, Anthropic, Gemini, Cohere, Groq, xAI, ElevenLabs, Jina, OpenRouter).
|
||||
---
|
||||
|
||||
# Developing with the Laravel AI SDK
|
||||
|
||||
The Laravel AI SDK (`laravel/ai`) is the official AI package for Laravel, providing a unified API for agents, images, audio, transcription, embeddings, reranking, vector stores, and file management across multiple AI providers.
|
||||
|
||||
## Searching the Documentation
|
||||
|
||||
This package is new. Always search the documentation before implementing any feature. Never guess at APIs — the documentation is the single source of truth.
|
||||
|
||||
- Use broad, simple queries that match the documentation section headings below.
|
||||
- Do not add package names to queries — package information is shared automatically. Use `test agent fake`, not `laravel ai test agent fake`.
|
||||
- Run multiple queries at once — the most relevant results are returned first.
|
||||
|
||||
### Documentation Sections
|
||||
|
||||
Use these section headings as query terms for accurate results:
|
||||
|
||||
- Introduction, Installation, Configuration, Provider Support
|
||||
- Agents: Prompting, Conversation Context, Structured Output, Attachments, Streaming, Broadcasting, Queueing, Tools, Provider Tools, Middleware, Anonymous Agents, Agent Configuration
|
||||
- Images
|
||||
- Audio (TTS)
|
||||
- Transcription (STT)
|
||||
- Embeddings: Querying Embeddings, Caching Embeddings
|
||||
- Reranking
|
||||
- Files
|
||||
- Vector Stores: Adding Files to Stores
|
||||
- Failover
|
||||
- Testing: Agents, Images, Audio, Transcriptions, Embeddings, Reranking, Files, Vector Stores
|
||||
- Events
|
||||
|
||||
## Decision Workflow
|
||||
|
||||
Determine the right entry point before writing code:
|
||||
|
||||
Text generation or chat? → Agent class with `Promptable` trait
|
||||
Chat with conversation history? → Agent + `Conversational` interface (manual) or `RemembersConversations` trait (automatic)
|
||||
Structured JSON output? → Agent + `HasStructuredOutput` interface
|
||||
Image generation? → `Image::of()->generate()`
|
||||
Audio synthesis? → `Audio::of()->generate()`
|
||||
Transcription? → `Transcription::fromPath()->generate()`
|
||||
Embeddings? → `Embeddings::for()->generate()`
|
||||
Reranking? → `Reranking::of()->rerank()`
|
||||
File storage? → `Document::fromPath()->put()`
|
||||
Vector stores? → `Stores::create()`
|
||||
|
||||
## Basic Usage Examples
|
||||
|
||||
### Agents
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Contracts\Agent;
|
||||
use Laravel\Ai\Promptable;
|
||||
|
||||
class SalesCoach implements Agent
|
||||
{
|
||||
use Promptable;
|
||||
|
||||
public function instructions(): string
|
||||
{
|
||||
return 'You are a sales coach.';
|
||||
}
|
||||
}
|
||||
|
||||
// Prompting
|
||||
$response = (new SalesCoach)->prompt('Analyze this transcript...');
|
||||
echo $response->text;
|
||||
|
||||
// Streaming (returns SSE response from a route)
|
||||
return (new SalesCoach)->stream('Analyze this transcript...');
|
||||
|
||||
// Queueing
|
||||
(new SalesCoach)->queue('Analyze this transcript...')
|
||||
->then(fn ($response) => /* ... */);
|
||||
|
||||
// Anonymous agents
|
||||
use function Laravel\Ai\{agent};
|
||||
|
||||
$response = agent(instructions: 'You are a helpful assistant.')->prompt('Hello');
|
||||
```
|
||||
|
||||
### Conversation Context
|
||||
|
||||
Manual conversation history via the `Conversational` interface:
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Contracts\Agent;
|
||||
use Laravel\Ai\Contracts\Conversational;
|
||||
use Laravel\Ai\Messages\Message;
|
||||
use Laravel\Ai\Promptable;
|
||||
|
||||
class SalesCoach implements Agent, Conversational
|
||||
{
|
||||
use Promptable;
|
||||
|
||||
public function __construct(public User $user) {}
|
||||
|
||||
public function instructions(): string { return 'You are a sales coach.'; }
|
||||
|
||||
public function messages(): iterable
|
||||
{
|
||||
return History::where('user_id', $this->user->id)
|
||||
->latest()->limit(50)->get()->reverse()
|
||||
->map(fn ($m) => new Message($m->role, $m->content))
|
||||
->all();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Automatic conversation persistence via the `RemembersConversations` trait:
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Concerns\RemembersConversations;
|
||||
use Laravel\Ai\Contracts\Agent;
|
||||
use Laravel\Ai\Contracts\Conversational;
|
||||
use Laravel\Ai\Promptable;
|
||||
|
||||
class SalesCoach implements Agent, Conversational
|
||||
{
|
||||
use Promptable, RemembersConversations;
|
||||
|
||||
public function instructions(): string { return 'You are a sales coach.'; }
|
||||
}
|
||||
|
||||
// Start a new conversation
|
||||
$response = (new SalesCoach)->forUser($user)->prompt('Hello!');
|
||||
$conversationId = $response->conversationId;
|
||||
|
||||
// Continue an existing conversation
|
||||
$response = (new SalesCoach)->continue($conversationId, as: $user)->prompt('Tell me more.');
|
||||
```
|
||||
|
||||
### Structured Output
|
||||
|
||||
```php
|
||||
use Illuminate\Contracts\JsonSchema\JsonSchema;
|
||||
use Laravel\Ai\Contracts\Agent;
|
||||
use Laravel\Ai\Contracts\HasStructuredOutput;
|
||||
use Laravel\Ai\Promptable;
|
||||
|
||||
class Reviewer implements Agent, HasStructuredOutput
|
||||
{
|
||||
use Promptable;
|
||||
|
||||
public function instructions(): string { return 'Review and score content.'; }
|
||||
|
||||
public function schema(JsonSchema $schema): array
|
||||
{
|
||||
return [
|
||||
'feedback' => $schema->string()->required(),
|
||||
'score' => $schema->integer()->min(1)->max(10)->required(),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$response = (new Reviewer)->prompt('Review this...');
|
||||
echo $response['score']; // Access like an array
|
||||
```
|
||||
|
||||
### Images
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Image;
|
||||
|
||||
$image = Image::of('A sunset over mountains')
|
||||
->landscape()
|
||||
->quality('high')
|
||||
->generate();
|
||||
|
||||
$path = $image->store(); // Store to default disk
|
||||
```
|
||||
|
||||
### Audio
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Audio;
|
||||
|
||||
$audio = Audio::of('Hello from Laravel.')
|
||||
->female()
|
||||
->instructions('Speak warmly')
|
||||
->generate();
|
||||
|
||||
$path = $audio->store();
|
||||
```
|
||||
|
||||
### Transcription
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Transcription;
|
||||
|
||||
$transcript = Transcription::fromStorage('audio.mp3')
|
||||
->diarize()
|
||||
->generate();
|
||||
|
||||
echo (string) $transcript;
|
||||
```
|
||||
|
||||
### Embeddings
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Embeddings;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
$response = Embeddings::for(['Text one', 'Text two'])
|
||||
->dimensions(1536)
|
||||
->cache()
|
||||
->generate();
|
||||
|
||||
// Single string via Stringable
|
||||
$embedding = Str::of('Napa Valley has great wine.')->toEmbeddings();
|
||||
```
|
||||
|
||||
### Reranking
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Reranking;
|
||||
|
||||
$response = Reranking::of(['Django is Python.', 'Laravel is PHP.', 'React is JS.'])
|
||||
->limit(5)
|
||||
->rerank('PHP frameworks');
|
||||
|
||||
$response->first()->document; // "Laravel is PHP."
|
||||
```
|
||||
|
||||
### Files and Vector Stores
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Files\Document;
|
||||
use Laravel\Ai\Stores;
|
||||
|
||||
// Store a file with the provider
|
||||
$file = Document::fromPath('/path/to/doc.pdf')->put();
|
||||
|
||||
// Create a vector store and add files
|
||||
$store = Stores::create('Knowledge Base');
|
||||
$store->add($file->id);
|
||||
$store->add(Document::fromStorage('manual.pdf')); // Store + add in one step
|
||||
```
|
||||
|
||||
## Agent Configuration
|
||||
|
||||
### PHP Attributes
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Attributes\{Provider, MaxSteps, MaxTokens, Temperature, Timeout};
|
||||
|
||||
#[Provider('anthropic')]
|
||||
#[MaxSteps(10)]
|
||||
#[MaxTokens(4096)]
|
||||
#[Temperature(0.7)]
|
||||
#[Timeout(120)]
|
||||
class MyAgent implements Agent
|
||||
{
|
||||
use Promptable;
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
The `#[UseCheapestModel]` and `#[UseSmartestModel]` attributes are also available for automatic model selection.
|
||||
|
||||
### Tools
|
||||
|
||||
Implement the `HasTools` interface and scaffold tools with `php artisan make:tool`:
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Contracts\HasTools;
|
||||
|
||||
class MyAgent implements Agent, HasTools
|
||||
{
|
||||
use Promptable;
|
||||
|
||||
public function tools(): iterable
|
||||
{
|
||||
return [new MyCustomTool];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### Provider Tools
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Providers\Tools\{WebSearch, WebFetch, FileSearch};
|
||||
|
||||
public function tools(): iterable
|
||||
{
|
||||
return [
|
||||
(new WebSearch)->max(5)->allow(['laravel.com']),
|
||||
new WebFetch,
|
||||
new FileSearch(stores: ['store_id']),
|
||||
];
|
||||
}
|
||||
```
|
||||
|
||||
### Conversation Memory
|
||||
|
||||
```php
|
||||
use Laravel\Ai\Concerns\RemembersConversations;
|
||||
use Laravel\Ai\Contracts\Conversational;
|
||||
|
||||
class ChatBot implements Agent, Conversational
|
||||
{
|
||||
use Promptable, RemembersConversations;
|
||||
// ...
|
||||
}
|
||||
|
||||
$response = (new ChatBot)->forUser($user)->prompt('Hello!');
|
||||
$response = (new ChatBot)->continue($conversationId, as: $user)->prompt('More...');
|
||||
```
|
||||
|
||||
### Failover
|
||||
|
||||
```php
|
||||
$response = (new MyAgent)->prompt('Hello', provider: ['openai', 'anthropic']);
|
||||
```
|
||||
|
||||
## Testing and Faking
|
||||
|
||||
Each capability supports `fake()` with assertions:
|
||||
|
||||
```php
|
||||
use App\Ai\Agents\SalesCoach;
|
||||
use Laravel\Ai\{Image, Audio, Transcription, Embeddings, Reranking, Files, Stores};
|
||||
|
||||
// Agents
|
||||
SalesCoach::fake(['Response 1', 'Response 2']);
|
||||
SalesCoach::assertPrompted('query');
|
||||
SalesCoach::assertNotPrompted('query');
|
||||
SalesCoach::assertNeverPrompted();
|
||||
SalesCoach::fake()->preventStrayPrompts();
|
||||
|
||||
// Images
|
||||
Image::fake();
|
||||
Image::assertGenerated(fn ($prompt) => $prompt->contains('sunset'));
|
||||
Image::assertNothingGenerated();
|
||||
|
||||
// Audio
|
||||
Audio::fake();
|
||||
Audio::assertGenerated(fn ($prompt) => $prompt->contains('Hello'));
|
||||
|
||||
// Transcription
|
||||
Transcription::fake(['Transcribed text.']);
|
||||
Transcription::assertGenerated(fn ($prompt) => $prompt->isDiarized());
|
||||
|
||||
// Embeddings
|
||||
Embeddings::fake();
|
||||
Embeddings::assertGenerated(fn ($prompt) => $prompt->contains('Laravel'));
|
||||
|
||||
// Reranking
|
||||
Reranking::fake();
|
||||
Reranking::assertReranked(fn ($prompt) => $prompt->contains('PHP'));
|
||||
|
||||
// Files
|
||||
Files::fake();
|
||||
Files::assertStored(fn ($file) => $file->mimeType() === 'text/plain');
|
||||
|
||||
// Stores
|
||||
Stores::fake();
|
||||
Stores::assertCreated('Knowledge Base');
|
||||
$store = Stores::get('id');
|
||||
$store->assertAdded('file_id');
|
||||
```
|
||||
|
||||
## Key Patterns
|
||||
|
||||
- Namespace: `Laravel\Ai\`
|
||||
- Package: `composer require laravel/ai`
|
||||
- Agent pattern: Implement the `Agent` interface and use the `Promptable` trait
|
||||
- Optional interfaces: `HasTools`, `HasMiddleware`, `HasStructuredOutput`, `Conversational`
|
||||
- Entry-point classes: `Image`, `Audio`, `Transcription`, `Embeddings`, `Reranking`, `Stores`
|
||||
- Artisan commands: `php artisan make:agent`, `php artisan make:tool`
|
||||
- Global helper: `agent()` for anonymous agents
|
||||
|
||||
## Common Pitfalls
|
||||
|
||||
### Wrong Namespace
|
||||
|
||||
The namespace is `Laravel\Ai`, not `Illuminate\Ai` or `Laravel\AI`.
|
||||
|
||||
```php
|
||||
// Correct
|
||||
use Laravel\Ai\Image;
|
||||
use Laravel\Ai\Contracts\Agent;
|
||||
use Laravel\Ai\Promptable;
|
||||
|
||||
// Wrong — these do not exist
|
||||
use Illuminate\Ai\Image;
|
||||
use Laravel\AI\Agent;
|
||||
```
|
||||
|
||||
### Unsupported Provider Capability
|
||||
|
||||
Calling a capability not supported by a provider throws a `LogicException`. Refer to the provider support table below.
|
||||
|
||||
### Never Use Prism Directly
|
||||
|
||||
Use agents and entry-point classes (`Image`, `Audio`, etc.) — not `Prism::text()` directly. The AI SDK wraps Prism internally.
|
||||
|
||||
## Provider Support
|
||||
|
||||
| Provider | Text | Image | Audio | STT | Embeddings | Reranking | Files | Stores |
|
||||
| ---------- | ---- | ----- | ----- | --- | ---------- | --------- | ----- | ------ |
|
||||
| OpenAI | Y | Y | Y | Y | Y | - | Y | Y |
|
||||
| Anthropic | Y | - | - | - | - | - | Y | - |
|
||||
| Gemini | Y | Y | - | - | Y | - | Y | Y |
|
||||
| xAI | Y | Y | - | - | - | - | - | - |
|
||||
| Groq | Y | - | - | - | - | - | - | - |
|
||||
| OpenRouter | Y | - | - | - | - | - | - | - |
|
||||
| ElevenLabs | - | - | Y | Y | - | - | - | - |
|
||||
| Cohere | - | - | - | - | Y | Y | - | - |
|
||||
| Jina | - | - | - | - | Y | Y | - | - |
|
||||
100
.github/skills/tailwindcss-development/SKILL.md
vendored
Normal file
100
.github/skills/tailwindcss-development/SKILL.md
vendored
Normal file
@ -0,0 +1,100 @@
|
||||
---
|
||||
name: tailwindcss-development
|
||||
description: "Styles applications using Tailwind CSS v3 utilities. Activates when adding styles, restyling components, working with gradients, spacing, layout, flex, grid, responsive design, dark mode, colors, typography, or borders; or when the user mentions CSS, styling, classes, Tailwind, restyle, hero section, cards, buttons, or any visual/UI changes."
|
||||
license: MIT
|
||||
metadata:
|
||||
author: laravel
|
||||
---
|
||||
|
||||
# Tailwind CSS Development
|
||||
|
||||
## When to Apply
|
||||
|
||||
Activate this skill when:
|
||||
- Adding styles to components or pages
|
||||
- Working with responsive design
|
||||
- Implementing dark mode
|
||||
- Extracting repeated patterns into components
|
||||
- Debugging spacing or layout issues
|
||||
|
||||
## Documentation
|
||||
|
||||
Use `search-docs` for detailed Tailwind CSS v3 patterns and documentation.
|
||||
|
||||
## Basic Usage
|
||||
|
||||
- Use Tailwind CSS classes to style HTML. Check and follow existing Tailwind conventions in the project before introducing new patterns.
|
||||
- Offer to extract repeated patterns into components that match the project's conventions (e.g., Blade, JSX, Vue).
|
||||
- Consider class placement, order, priority, and defaults. Remove redundant classes, add classes to parent or child elements carefully to reduce repetition, and group elements logically.
|
||||
|
||||
## Tailwind CSS v3 Specifics
|
||||
|
||||
- Always use Tailwind CSS v3 and verify you're using only classes it supports.
|
||||
- Configuration is done in the `tailwind.config.js` file.
|
||||
- Import using `@tailwind` directives:
|
||||
|
||||
<!-- v3 Import Syntax -->
|
||||
```css
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
```
|
||||
|
||||
## Spacing
|
||||
|
||||
When listing items, use gap utilities for spacing; don't use margins.
|
||||
|
||||
<!-- Gap Utilities -->
|
||||
```html
|
||||
<div class="flex gap-8">
|
||||
<div>Item 1</div>
|
||||
<div>Item 2</div>
|
||||
</div>
|
||||
```
|
||||
|
||||
## Dark Mode
|
||||
|
||||
If existing pages and components support dark mode, new pages and components must support it the same way, typically using the `dark:` variant:
|
||||
|
||||
<!-- Dark Mode -->
|
||||
```html
|
||||
<div class="bg-white dark:bg-gray-900 text-gray-900 dark:text-white">
|
||||
Content adapts to color scheme
|
||||
</div>
|
||||
```
|
||||
|
||||
## Common Patterns
|
||||
|
||||
### Flexbox Layout
|
||||
|
||||
<!-- Flexbox Layout -->
|
||||
```html
|
||||
<div class="flex items-center justify-between gap-4">
|
||||
<div>Left content</div>
|
||||
<div>Right content</div>
|
||||
</div>
|
||||
```
|
||||
|
||||
### Grid Layout
|
||||
|
||||
<!-- Grid Layout -->
|
||||
```html
|
||||
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
|
||||
<div>Card 1</div>
|
||||
<div>Card 2</div>
|
||||
<div>Card 3</div>
|
||||
</div>
|
||||
```
|
||||
|
||||
## Verification
|
||||
|
||||
1. Check browser for visual rendering
|
||||
2. Test responsive breakpoints
|
||||
3. Verify dark mode if project uses it
|
||||
|
||||
## Common Pitfalls
|
||||
|
||||
- Using margins for spacing between siblings instead of gap utilities
|
||||
- Forgetting to add dark mode variants when the project uses dark mode
|
||||
- Not checking existing project conventions before adding new utilities
|
||||
- Overusing inline styles when Tailwind classes would suffice
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@ -28,3 +28,4 @@ Thumbs.db
|
||||
composer.lock
|
||||
.codex/config.toml
|
||||
/public/vendor/
|
||||
package-lock.json
|
||||
|
||||
415
AGENTS.md
415
AGENTS.md
@ -5,3 +5,418 @@ Act as a Senior Laravel & FilamentPHP Architect. Refactor the attached code as a
|
||||
4. Database: Consolidate migrations into a single file per table or topic (e.g., users, cache, jobs) to reduce the overall number of migration files.
|
||||
5. Modularity: Use the `laravel-modules` package to encapsulate all features, routing, and Filament resources strictly inside their respective modules.
|
||||
6. Frontend: Optimize and reduce the CSS footprint while maintaining the exact same visual output.
|
||||
|
||||
===
|
||||
|
||||
<laravel-boost-guidelines>
|
||||
=== foundation rules ===
|
||||
|
||||
# Laravel Boost Guidelines
|
||||
|
||||
The Laravel Boost guidelines are specifically curated by Laravel maintainers for this application. These guidelines should be followed closely to ensure the best experience when building Laravel applications.
|
||||
|
||||
## Foundational Context
|
||||
|
||||
This application is a Laravel application and its main Laravel ecosystems package & versions are below. You are an expert with them all. Ensure you abide by these specific packages & versions.
|
||||
|
||||
- php - 8.4.18
|
||||
- filament/filament (FILAMENT) - v5
|
||||
- laravel/ai (AI) - v0
|
||||
- laravel/framework (LARAVEL) - v12
|
||||
- laravel/prompts (PROMPTS) - v0
|
||||
- laravel/reverb (REVERB) - v1
|
||||
- laravel/sanctum (SANCTUM) - v4
|
||||
- laravel/socialite (SOCIALITE) - v5
|
||||
- livewire/livewire (LIVEWIRE) - v4
|
||||
- laravel/boost (BOOST) - v2
|
||||
- laravel/mcp (MCP) - v0
|
||||
- laravel/pail (PAIL) - v1
|
||||
- laravel/pint (PINT) - v1
|
||||
- laravel/sail (SAIL) - v1
|
||||
- phpunit/phpunit (PHPUNIT) - v11
|
||||
- alpinejs (ALPINEJS) - v3
|
||||
- laravel-echo (ECHO) - v2
|
||||
- tailwindcss (TAILWINDCSS) - v3
|
||||
|
||||
## Skills Activation
|
||||
|
||||
This project has domain-specific skills available. You MUST activate the relevant skill whenever you work in that domain—don't wait until you're stuck.
|
||||
|
||||
- `tailwindcss-development` — Styles applications using Tailwind CSS v3 utilities. Activates when adding styles, restyling components, working with gradients, spacing, layout, flex, grid, responsive design, dark mode, colors, typography, or borders; or when the user mentions CSS, styling, classes, Tailwind, restyle, hero section, cards, buttons, or any visual/UI changes.
|
||||
- `developing-with-ai-sdk` — Builds AI agents, generates text and chat responses, produces images, synthesizes audio, transcribes speech, generates vector embeddings, reranks documents, and manages files and vector stores using the Laravel AI SDK (laravel/ai). Supports structured output, streaming, tools, conversation memory, middleware, queueing, broadcasting, and provider failover. Use when building, editing, updating, debugging, or testing any AI functionality, including agents, LLMs, chatbots, text generation, image generation, audio, transcription, embeddings, RAG, similarity search, vector stores, prompting, structured output, or any AI provider (OpenAI, Anthropic, Gemini, Cohere, Groq, xAI, ElevenLabs, Jina, OpenRouter).
|
||||
|
||||
## Conventions
|
||||
|
||||
- You must follow all existing code conventions used in this application. When creating or editing a file, check sibling files for the correct structure, approach, and naming.
|
||||
- Use descriptive names for variables and methods. For example, `isRegisteredForDiscounts`, not `discount()`.
|
||||
- Check for existing components to reuse before writing a new one.
|
||||
|
||||
## Verification Scripts
|
||||
|
||||
- Do not create verification scripts or tinker when tests cover that functionality and prove they work. Unit and feature tests are more important.
|
||||
|
||||
## Application Structure & Architecture
|
||||
|
||||
- Stick to existing directory structure; don't create new base folders without approval.
|
||||
- Do not change the application's dependencies without approval.
|
||||
|
||||
## Frontend Bundling
|
||||
|
||||
- If the user doesn't see a frontend change reflected in the UI, it could mean they need to run `npm run build`, `npm run dev`, or `composer run dev`. Ask them.
|
||||
|
||||
## Documentation Files
|
||||
|
||||
- You must only create documentation files if explicitly requested by the user.
|
||||
|
||||
## Replies
|
||||
|
||||
- Be concise in your explanations - focus on what's important rather than explaining obvious details.
|
||||
|
||||
=== boost rules ===
|
||||
|
||||
# Laravel Boost
|
||||
|
||||
- Laravel Boost is an MCP server that comes with powerful tools designed specifically for this application. Use them.
|
||||
|
||||
## Artisan
|
||||
|
||||
- Use the `list-artisan-commands` tool when you need to call an Artisan command to double-check the available parameters.
|
||||
|
||||
## URLs
|
||||
|
||||
- Whenever you share a project URL with the user, you should use the `get-absolute-url` tool to ensure you're using the correct scheme, domain/IP, and port.
|
||||
|
||||
## Tinker / Debugging
|
||||
|
||||
- You should use the `tinker` tool when you need to execute PHP to debug code or query Eloquent models directly.
|
||||
- Use the `database-query` tool when you only need to read from the database.
|
||||
- Use the `database-schema` tool to inspect table structure before writing migrations or models.
|
||||
|
||||
## Reading Browser Logs With the `browser-logs` Tool
|
||||
|
||||
- You can read browser logs, errors, and exceptions using the `browser-logs` tool from Boost.
|
||||
- Only recent browser logs will be useful - ignore old logs.
|
||||
|
||||
## Searching Documentation (Critically Important)
|
||||
|
||||
- Boost comes with a powerful `search-docs` tool you should use before trying other approaches when working with Laravel or Laravel ecosystem packages. This tool automatically passes a list of installed packages and their versions to the remote Boost API, so it returns only version-specific documentation for the user's circumstance. You should pass an array of packages to filter on if you know you need docs for particular packages.
|
||||
- Search the documentation before making code changes to ensure we are taking the correct approach.
|
||||
- Use multiple, broad, simple, topic-based queries at once. For example: `['rate limiting', 'routing rate limiting', 'routing']`. The most relevant results will be returned first.
|
||||
- Do not add package names to queries; package information is already shared. For example, use `test resource table`, not `filament 4 test resource table`.
|
||||
|
||||
### Available Search Syntax
|
||||
|
||||
1. Simple Word Searches with auto-stemming - query=authentication - finds 'authenticate' and 'auth'.
|
||||
2. Multiple Words (AND Logic) - query=rate limit - finds knowledge containing both "rate" AND "limit".
|
||||
3. Quoted Phrases (Exact Position) - query="infinite scroll" - words must be adjacent and in that order.
|
||||
4. Mixed Queries - query=middleware "rate limit" - "middleware" AND exact phrase "rate limit".
|
||||
5. Multiple Queries - queries=["authentication", "middleware"] - ANY of these terms.
|
||||
|
||||
=== php rules ===
|
||||
|
||||
# PHP
|
||||
|
||||
- Always use curly braces for control structures, even for single-line bodies.
|
||||
|
||||
## Constructors
|
||||
|
||||
- Use PHP 8 constructor property promotion in `__construct()`.
|
||||
- `public function __construct(public GitHub $github) { }`
|
||||
- Do not allow empty `__construct()` methods with zero parameters unless the constructor is private.
|
||||
|
||||
## Type Declarations
|
||||
|
||||
- Always use explicit return type declarations for methods and functions.
|
||||
- Use appropriate PHP type hints for method parameters.
|
||||
|
||||
<!-- Explicit Return Types and Method Params -->
|
||||
```php
|
||||
protected function isAccessible(User $user, ?string $path = null): bool
|
||||
{
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
## Enums
|
||||
|
||||
- Typically, keys in an Enum should be TitleCase. For example: `FavoritePerson`, `BestLake`, `Monthly`.
|
||||
|
||||
## Comments
|
||||
|
||||
- Prefer PHPDoc blocks over inline comments. Never use comments within the code itself unless the logic is exceptionally complex.
|
||||
|
||||
## PHPDoc Blocks
|
||||
|
||||
- Add useful array shape type definitions when appropriate.
|
||||
|
||||
=== laravel/core rules ===
|
||||
|
||||
# Do Things the Laravel Way
|
||||
|
||||
- Use `php artisan make:` commands to create new files (i.e. migrations, controllers, models, etc.). You can list available Artisan commands using the `list-artisan-commands` tool.
|
||||
- If you're creating a generic PHP class, use `php artisan make:class`.
|
||||
- Pass `--no-interaction` to all Artisan commands to ensure they work without user input. You should also pass the correct `--options` to ensure correct behavior.
|
||||
|
||||
## Database
|
||||
|
||||
- Always use proper Eloquent relationship methods with return type hints. Prefer relationship methods over raw queries or manual joins.
|
||||
- Use Eloquent models and relationships before suggesting raw database queries.
|
||||
- Avoid `DB::`; prefer `Model::query()`. Generate code that leverages Laravel's ORM capabilities rather than bypassing them.
|
||||
- Generate code that prevents N+1 query problems by using eager loading.
|
||||
- Use Laravel's query builder for very complex database operations.
|
||||
|
||||
### Model Creation
|
||||
|
||||
- When creating new models, create useful factories and seeders for them too. Ask the user if they need any other things, using `list-artisan-commands` to check the available options to `php artisan make:model`.
|
||||
|
||||
### APIs & Eloquent Resources
|
||||
|
||||
- For APIs, default to using Eloquent API Resources and API versioning unless existing API routes do not, then you should follow existing application convention.
|
||||
|
||||
## Controllers & Validation
|
||||
|
||||
- Always create Form Request classes for validation rather than inline validation in controllers. Include both validation rules and custom error messages.
|
||||
- Check sibling Form Requests to see if the application uses array or string based validation rules.
|
||||
|
||||
## Authentication & Authorization
|
||||
|
||||
- Use Laravel's built-in authentication and authorization features (gates, policies, Sanctum, etc.).
|
||||
|
||||
## URL Generation
|
||||
|
||||
- When generating links to other pages, prefer named routes and the `route()` function.
|
||||
|
||||
## Queues
|
||||
|
||||
- Use queued jobs for time-consuming operations with the `ShouldQueue` interface.
|
||||
|
||||
## Configuration
|
||||
|
||||
- Use environment variables only in configuration files - never use the `env()` function directly outside of config files. Always use `config('app.name')`, not `env('APP_NAME')`.
|
||||
|
||||
## Testing
|
||||
|
||||
- When creating models for tests, use the factories for the models. Check if the factory has custom states that can be used before manually setting up the model.
|
||||
- Faker: Use methods such as `$this->faker->word()` or `fake()->randomDigit()`. Follow existing conventions whether to use `$this->faker` or `fake()`.
|
||||
- When creating tests, make use of `php artisan make:test [options] {name}` to create a feature test, and pass `--unit` to create a unit test. Most tests should be feature tests.
|
||||
|
||||
## Vite Error
|
||||
|
||||
- If you receive an "Illuminate\Foundation\ViteException: Unable to locate file in Vite manifest" error, you can run `npm run build` or ask the user to run `npm run dev` or `composer run dev`.
|
||||
|
||||
=== laravel/v12 rules ===
|
||||
|
||||
# Laravel 12
|
||||
|
||||
- CRITICAL: ALWAYS use `search-docs` tool for version-specific Laravel documentation and updated code examples.
|
||||
- Since Laravel 11, Laravel has a new streamlined file structure which this project uses.
|
||||
|
||||
## Laravel 12 Structure
|
||||
|
||||
- In Laravel 12, middleware are no longer registered in `app/Http/Kernel.php`.
|
||||
- Middleware are configured declaratively in `bootstrap/app.php` using `Application::configure()->withMiddleware()`.
|
||||
- `bootstrap/app.php` is the file to register middleware, exceptions, and routing files.
|
||||
- `bootstrap/providers.php` contains application specific service providers.
|
||||
- The `app\Console\Kernel.php` file no longer exists; use `bootstrap/app.php` or `routes/console.php` for console configuration.
|
||||
- Console commands in `app/Console/Commands/` are automatically available and do not require manual registration.
|
||||
|
||||
## Database
|
||||
|
||||
- When modifying a column, the migration must include all of the attributes that were previously defined on the column. Otherwise, they will be dropped and lost.
|
||||
- Laravel 12 allows limiting eagerly loaded records natively, without external packages: `$query->latest()->limit(10);`.
|
||||
|
||||
### Models
|
||||
|
||||
- Casts can and likely should be set in a `casts()` method on a model rather than the `$casts` property. Follow existing conventions from other models.
|
||||
|
||||
=== pint/core rules ===
|
||||
|
||||
# Laravel Pint Code Formatter
|
||||
|
||||
- If you have modified any PHP files, you must run `vendor/bin/pint --dirty --format agent` before finalizing changes to ensure your code matches the project's expected style.
|
||||
- Do not run `vendor/bin/pint --test --format agent`, simply run `vendor/bin/pint --format agent` to fix any formatting issues.
|
||||
|
||||
=== phpunit/core rules ===
|
||||
|
||||
# PHPUnit
|
||||
|
||||
- This application uses PHPUnit for testing. All tests must be written as PHPUnit classes. Use `php artisan make:test --phpunit {name}` to create a new test.
|
||||
- If you see a test using "Pest", convert it to PHPUnit.
|
||||
- Every time a test has been updated, run that singular test.
|
||||
- When the tests relating to your feature are passing, ask the user if they would like to also run the entire test suite to make sure everything is still passing.
|
||||
- Tests should cover all happy paths, failure paths, and edge cases.
|
||||
- You must not remove any tests or test files from the tests directory without approval. These are not temporary or helper files; these are core to the application.
|
||||
|
||||
## Running Tests
|
||||
|
||||
- Run the minimal number of tests, using an appropriate filter, before finalizing.
|
||||
- To run all tests: `php artisan test --compact`.
|
||||
- To run all tests in a file: `php artisan test --compact tests/Feature/ExampleTest.php`.
|
||||
- To filter on a particular test name: `php artisan test --compact --filter=testName` (recommended after making a change to a related file).
|
||||
|
||||
=== tailwindcss/core rules ===
|
||||
|
||||
# Tailwind CSS
|
||||
|
||||
- Always use existing Tailwind conventions; check project patterns before adding new ones.
|
||||
- IMPORTANT: Always use `search-docs` tool for version-specific Tailwind CSS documentation and updated code examples. Never rely on training data.
|
||||
- IMPORTANT: Activate `tailwindcss-development` every time you're working with a Tailwind CSS or styling-related task.
|
||||
|
||||
=== filament/filament rules ===
|
||||
|
||||
## Filament
|
||||
|
||||
- Filament is used by this application. Follow the existing conventions for how and where it is implemented.
|
||||
- Filament is a Server-Driven UI (SDUI) framework for Laravel that lets you define user interfaces in PHP using structured configuration objects. Built on Livewire, Alpine.js, and Tailwind CSS.
|
||||
- Use the `search-docs` tool for official documentation on Artisan commands, code examples, testing, relationships, and idiomatic practices. If `search-docs` is unavailable, refer to https://filamentphp.com/docs.
|
||||
|
||||
### Artisan
|
||||
|
||||
- Always use Filament-specific Artisan commands to create files. Find available commands with the `list-artisan-commands` tool, or run `php artisan --help`.
|
||||
- Always inspect required options before running a command, and always pass `--no-interaction`.
|
||||
|
||||
### Patterns
|
||||
|
||||
Always use static `make()` methods to initialize components. Most configuration methods accept a `Closure` for dynamic values.
|
||||
|
||||
Use `Get $get` to read other form field values for conditional logic:
|
||||
|
||||
<code-snippet name="Conditional form field visibility" lang="php">
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
|
||||
Select::make('type')
|
||||
->options(CompanyType::class)
|
||||
->required()
|
||||
->live(),
|
||||
|
||||
TextInput::make('company_name')
|
||||
->required()
|
||||
->visible(fn (Get $get): bool => $get('type') === 'business'),
|
||||
|
||||
</code-snippet>
|
||||
|
||||
Use `state()` with a `Closure` to compute derived column values:
|
||||
|
||||
<code-snippet name="Computed table column value" lang="php">
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
|
||||
TextColumn::make('full_name')
|
||||
->state(fn (User $record): string => "{$record->first_name} {$record->last_name}"),
|
||||
|
||||
</code-snippet>
|
||||
|
||||
Actions encapsulate a button with an optional modal form and logic:
|
||||
|
||||
<code-snippet name="Action with modal form" lang="php">
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
|
||||
Action::make('updateEmail')
|
||||
->schema([
|
||||
TextInput::make('email')
|
||||
->email()
|
||||
->required(),
|
||||
])
|
||||
->action(fn (array $data, User $record) => $record->update($data))
|
||||
|
||||
</code-snippet>
|
||||
|
||||
### Testing
|
||||
|
||||
Always authenticate before testing panel functionality. Filament uses Livewire, so use `Livewire::test()` or `livewire()` (available when `pestphp/pest-plugin-livewire` is in `composer.json`):
|
||||
|
||||
<code-snippet name="Table test" lang="php">
|
||||
use function Pest\Livewire\livewire;
|
||||
|
||||
livewire(ListUsers::class)
|
||||
->assertCanSeeTableRecords($users)
|
||||
->searchTable($users->first()->name)
|
||||
->assertCanSeeTableRecords($users->take(1))
|
||||
->assertCanNotSeeTableRecords($users->skip(1));
|
||||
|
||||
</code-snippet>
|
||||
|
||||
<code-snippet name="Create resource test" lang="php">
|
||||
use function Pest\Laravel\assertDatabaseHas;
|
||||
use function Pest\Livewire\livewire;
|
||||
|
||||
livewire(CreateUser::class)
|
||||
->fillForm([
|
||||
'name' => 'Test',
|
||||
'email' => 'test@example.com',
|
||||
])
|
||||
->call('create')
|
||||
->assertNotified()
|
||||
->assertRedirect();
|
||||
|
||||
assertDatabaseHas(User::class, [
|
||||
'name' => 'Test',
|
||||
'email' => 'test@example.com',
|
||||
]);
|
||||
|
||||
</code-snippet>
|
||||
|
||||
<code-snippet name="Testing validation" lang="php">
|
||||
use function Pest\Livewire\livewire;
|
||||
|
||||
livewire(CreateUser::class)
|
||||
->fillForm([
|
||||
'name' => null,
|
||||
'email' => 'invalid-email',
|
||||
])
|
||||
->call('create')
|
||||
->assertHasFormErrors([
|
||||
'name' => 'required',
|
||||
'email' => 'email',
|
||||
])
|
||||
->assertNotNotified();
|
||||
|
||||
</code-snippet>
|
||||
|
||||
<code-snippet name="Calling actions in pages" lang="php">
|
||||
use Filament\Actions\DeleteAction;
|
||||
use function Pest\Livewire\livewire;
|
||||
|
||||
livewire(EditUser::class, ['record' => $user->id])
|
||||
->callAction(DeleteAction::class)
|
||||
->assertNotified()
|
||||
->assertRedirect();
|
||||
|
||||
</code-snippet>
|
||||
|
||||
<code-snippet name="Calling actions in tables" lang="php">
|
||||
use Filament\Actions\Testing\TestAction;
|
||||
use function Pest\Livewire\livewire;
|
||||
|
||||
livewire(ListUsers::class)
|
||||
->callAction(TestAction::make('promote')->table($user), [
|
||||
'role' => 'admin',
|
||||
])
|
||||
->assertNotified();
|
||||
|
||||
</code-snippet>
|
||||
|
||||
### Correct Namespaces
|
||||
|
||||
- Form fields (`TextInput`, `Select`, etc.): `Filament\Forms\Components\`
|
||||
- Infolist entries (`TextEntry`, `IconEntry`, etc.): `Filament\Infolists\Components\`
|
||||
- Layout components (`Grid`, `Section`, `Fieldset`, `Tabs`, `Wizard`, etc.): `Filament\Schemas\Components\`
|
||||
- Schema utilities (`Get`, `Set`, etc.): `Filament\Schemas\Components\Utilities\`
|
||||
- Actions (`DeleteAction`, `CreateAction`, etc.): `Filament\Actions\`. Never use `Filament\Tables\Actions\`, `Filament\Forms\Actions\`, or any other sub-namespace for actions.
|
||||
- Icons: `Filament\Support\Icons\Heroicon` enum (e.g., `Heroicon::PencilSquare`)
|
||||
|
||||
### Common Mistakes
|
||||
|
||||
- **Never assume public file visibility.** File visibility is `private` by default. Always use `->visibility('public')` when public access is needed.
|
||||
- **Never assume full-width layout.** `Grid`, `Section`, and `Fieldset` do not span all columns by default. Explicitly set column spans when needed.
|
||||
|
||||
=== laravel/ai rules ===
|
||||
|
||||
## Laravel AI SDK
|
||||
|
||||
- This application uses the Laravel AI SDK (`laravel/ai`) for all AI functionality.
|
||||
- Activate the `developing-with-ai-sdk` skill when building, editing, updating, debugging, or testing AI agents, text generation, chat, streaming, structured output, tools, image generation, audio, transcription, embeddings, reranking, vector stores, files, conversation memory, or any AI provider integration (OpenAI, Anthropic, Gemini, Cohere, Groq, xAI, ElevenLabs, Jina, OpenRouter).
|
||||
|
||||
</laravel-boost-guidelines>
|
||||
|
||||
423
GEMINI.md
Normal file
423
GEMINI.md
Normal file
@ -0,0 +1,423 @@
|
||||
Act as a Senior Laravel & FilamentPHP Architect. Refactor the attached code as a greenfield project adhering to the following strict constraints:
|
||||
1. Architecture: Enforce strict SOLID principles, prioritize brevity, and completely ignore backward compatibility.
|
||||
2. Cleanup: Remove all legacy code, comments, tests, and PHPDocs.
|
||||
3. Refactoring: Move all database logic into Models and extract repetitive Filament code into dedicated Helper classes. Identify and fix any existing logical errors.
|
||||
4. Database: Consolidate migrations into a single file per table or topic (e.g., users, cache, jobs) to reduce the overall number of migration files.
|
||||
5. Modularity: Use the `laravel-modules` package to encapsulate all features, routing, and Filament resources strictly inside their respective modules.
|
||||
6. Frontend: Optimize and reduce the CSS footprint while maintaining the exact same visual output.
|
||||
7. Tooling: Use Laravel Boost as the default framework intelligence layer. Search Laravel Boost documentation before framework-level changes and prefer Boost MCP tools for artisan, browser logs, database inspection, and debugging.
|
||||
|
||||
===
|
||||
|
||||
<laravel-boost-guidelines>
|
||||
=== foundation rules ===
|
||||
|
||||
# Laravel Boost Guidelines
|
||||
|
||||
The Laravel Boost guidelines are specifically curated by Laravel maintainers for this application. These guidelines should be followed closely to ensure the best experience when building Laravel applications.
|
||||
|
||||
## Foundational Context
|
||||
|
||||
This application is a Laravel application and its main Laravel ecosystems package & versions are below. You are an expert with them all. Ensure you abide by these specific packages & versions.
|
||||
|
||||
- php - 8.4.18
|
||||
- filament/filament (FILAMENT) - v5
|
||||
- laravel/ai (AI) - v0
|
||||
- laravel/framework (LARAVEL) - v12
|
||||
- laravel/prompts (PROMPTS) - v0
|
||||
- laravel/reverb (REVERB) - v1
|
||||
- laravel/sanctum (SANCTUM) - v4
|
||||
- laravel/socialite (SOCIALITE) - v5
|
||||
- livewire/livewire (LIVEWIRE) - v4
|
||||
- laravel/boost (BOOST) - v2
|
||||
- laravel/mcp (MCP) - v0
|
||||
- laravel/pail (PAIL) - v1
|
||||
- laravel/pint (PINT) - v1
|
||||
- laravel/sail (SAIL) - v1
|
||||
- phpunit/phpunit (PHPUNIT) - v11
|
||||
- alpinejs (ALPINEJS) - v3
|
||||
- laravel-echo (ECHO) - v2
|
||||
- tailwindcss (TAILWINDCSS) - v3
|
||||
|
||||
## Skills Activation
|
||||
|
||||
This project has domain-specific skills available. You MUST activate the relevant skill whenever you work in that domain—don't wait until you're stuck.
|
||||
|
||||
- `tailwindcss-development` — Styles applications using Tailwind CSS v3 utilities. Activates when adding styles, restyling components, working with gradients, spacing, layout, flex, grid, responsive design, dark mode, colors, typography, or borders; or when the user mentions CSS, styling, classes, Tailwind, restyle, hero section, cards, buttons, or any visual/UI changes.
|
||||
- `developing-with-ai-sdk` — Builds AI agents, generates text and chat responses, produces images, synthesizes audio, transcribes speech, generates vector embeddings, reranks documents, and manages files and vector stores using the Laravel AI SDK (laravel/ai). Supports structured output, streaming, tools, conversation memory, middleware, queueing, broadcasting, and provider failover. Use when building, editing, updating, debugging, or testing any AI functionality, including agents, LLMs, chatbots, text generation, image generation, audio, transcription, embeddings, RAG, similarity search, vector stores, prompting, structured output, or any AI provider (OpenAI, Anthropic, Gemini, Cohere, Groq, xAI, ElevenLabs, Jina, OpenRouter).
|
||||
|
||||
## Conventions
|
||||
|
||||
- You must follow all existing code conventions used in this application. When creating or editing a file, check sibling files for the correct structure, approach, and naming.
|
||||
- Use descriptive names for variables and methods. For example, `isRegisteredForDiscounts`, not `discount()`.
|
||||
- Check for existing components to reuse before writing a new one.
|
||||
|
||||
## Verification Scripts
|
||||
|
||||
- Do not create verification scripts or tinker when tests cover that functionality and prove they work. Unit and feature tests are more important.
|
||||
|
||||
## Application Structure & Architecture
|
||||
|
||||
- Stick to existing directory structure; don't create new base folders without approval.
|
||||
- Do not change the application's dependencies without approval.
|
||||
|
||||
## Frontend Bundling
|
||||
|
||||
- If the user doesn't see a frontend change reflected in the UI, it could mean they need to run `npm run build`, `npm run dev`, or `composer run dev`. Ask them.
|
||||
|
||||
## Documentation Files
|
||||
|
||||
- You must only create documentation files if explicitly requested by the user.
|
||||
|
||||
## Replies
|
||||
|
||||
- Be concise in your explanations - focus on what's important rather than explaining obvious details.
|
||||
|
||||
=== boost rules ===
|
||||
|
||||
# Laravel Boost
|
||||
|
||||
- Laravel Boost is an MCP server that comes with powerful tools designed specifically for this application. Use them.
|
||||
|
||||
## Artisan
|
||||
|
||||
- Use the `list-artisan-commands` tool when you need to call an Artisan command to double-check the available parameters.
|
||||
|
||||
## URLs
|
||||
|
||||
- Whenever you share a project URL with the user, you should use the `get-absolute-url` tool to ensure you're using the correct scheme, domain/IP, and port.
|
||||
|
||||
## Tinker / Debugging
|
||||
|
||||
- You should use the `tinker` tool when you need to execute PHP to debug code or query Eloquent models directly.
|
||||
- Use the `database-query` tool when you only need to read from the database.
|
||||
- Use the `database-schema` tool to inspect table structure before writing migrations or models.
|
||||
|
||||
## Reading Browser Logs With the `browser-logs` Tool
|
||||
|
||||
- You can read browser logs, errors, and exceptions using the `browser-logs` tool from Boost.
|
||||
- Only recent browser logs will be useful - ignore old logs.
|
||||
|
||||
## Searching Documentation (Critically Important)
|
||||
|
||||
- Boost comes with a powerful `search-docs` tool you should use before trying other approaches when working with Laravel or Laravel ecosystem packages. This tool automatically passes a list of installed packages and their versions to the remote Boost API, so it returns only version-specific documentation for the user's circumstance. You should pass an array of packages to filter on if you know you need docs for particular packages.
|
||||
- Search the documentation before making code changes to ensure we are taking the correct approach.
|
||||
- Use multiple, broad, simple, topic-based queries at once. For example: `['rate limiting', 'routing rate limiting', 'routing']`. The most relevant results will be returned first.
|
||||
- Do not add package names to queries; package information is already shared. For example, use `test resource table`, not `filament 4 test resource table`.
|
||||
|
||||
### Available Search Syntax
|
||||
|
||||
1. Simple Word Searches with auto-stemming - query=authentication - finds 'authenticate' and 'auth'.
|
||||
2. Multiple Words (AND Logic) - query=rate limit - finds knowledge containing both "rate" AND "limit".
|
||||
3. Quoted Phrases (Exact Position) - query="infinite scroll" - words must be adjacent and in that order.
|
||||
4. Mixed Queries - query=middleware "rate limit" - "middleware" AND exact phrase "rate limit".
|
||||
5. Multiple Queries - queries=["authentication", "middleware"] - ANY of these terms.
|
||||
|
||||
=== php rules ===
|
||||
|
||||
# PHP
|
||||
|
||||
- Always use curly braces for control structures, even for single-line bodies.
|
||||
|
||||
## Constructors
|
||||
|
||||
- Use PHP 8 constructor property promotion in `__construct()`.
|
||||
- `public function __construct(public GitHub $github) { }`
|
||||
- Do not allow empty `__construct()` methods with zero parameters unless the constructor is private.
|
||||
|
||||
## Type Declarations
|
||||
|
||||
- Always use explicit return type declarations for methods and functions.
|
||||
- Use appropriate PHP type hints for method parameters.
|
||||
|
||||
<!-- Explicit Return Types and Method Params -->
|
||||
```php
|
||||
protected function isAccessible(User $user, ?string $path = null): bool
|
||||
{
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
## Enums
|
||||
|
||||
- Typically, keys in an Enum should be TitleCase. For example: `FavoritePerson`, `BestLake`, `Monthly`.
|
||||
|
||||
## Comments
|
||||
|
||||
- Prefer PHPDoc blocks over inline comments. Never use comments within the code itself unless the logic is exceptionally complex.
|
||||
|
||||
## PHPDoc Blocks
|
||||
|
||||
- Add useful array shape type definitions when appropriate.
|
||||
|
||||
=== laravel/core rules ===
|
||||
|
||||
# Do Things the Laravel Way
|
||||
|
||||
- Use `php artisan make:` commands to create new files (i.e. migrations, controllers, models, etc.). You can list available Artisan commands using the `list-artisan-commands` tool.
|
||||
- If you're creating a generic PHP class, use `php artisan make:class`.
|
||||
- Pass `--no-interaction` to all Artisan commands to ensure they work without user input. You should also pass the correct `--options` to ensure correct behavior.
|
||||
|
||||
## Database
|
||||
|
||||
- Always use proper Eloquent relationship methods with return type hints. Prefer relationship methods over raw queries or manual joins.
|
||||
- Use Eloquent models and relationships before suggesting raw database queries.
|
||||
- Avoid `DB::`; prefer `Model::query()`. Generate code that leverages Laravel's ORM capabilities rather than bypassing them.
|
||||
- Generate code that prevents N+1 query problems by using eager loading.
|
||||
- Use Laravel's query builder for very complex database operations.
|
||||
|
||||
### Model Creation
|
||||
|
||||
- When creating new models, create useful factories and seeders for them too. Ask the user if they need any other things, using `list-artisan-commands` to check the available options to `php artisan make:model`.
|
||||
|
||||
### APIs & Eloquent Resources
|
||||
|
||||
- For APIs, default to using Eloquent API Resources and API versioning unless existing API routes do not, then you should follow existing application convention.
|
||||
|
||||
## Controllers & Validation
|
||||
|
||||
- Always create Form Request classes for validation rather than inline validation in controllers. Include both validation rules and custom error messages.
|
||||
- Check sibling Form Requests to see if the application uses array or string based validation rules.
|
||||
|
||||
## Authentication & Authorization
|
||||
|
||||
- Use Laravel's built-in authentication and authorization features (gates, policies, Sanctum, etc.).
|
||||
|
||||
## URL Generation
|
||||
|
||||
- When generating links to other pages, prefer named routes and the `route()` function.
|
||||
|
||||
## Queues
|
||||
|
||||
- Use queued jobs for time-consuming operations with the `ShouldQueue` interface.
|
||||
|
||||
## Configuration
|
||||
|
||||
- Use environment variables only in configuration files - never use the `env()` function directly outside of config files. Always use `config('app.name')`, not `env('APP_NAME')`.
|
||||
|
||||
## Testing
|
||||
|
||||
- When creating models for tests, use the factories for the models. Check if the factory has custom states that can be used before manually setting up the model.
|
||||
- Faker: Use methods such as `$this->faker->word()` or `fake()->randomDigit()`. Follow existing conventions whether to use `$this->faker` or `fake()`.
|
||||
- When creating tests, make use of `php artisan make:test [options] {name}` to create a feature test, and pass `--unit` to create a unit test. Most tests should be feature tests.
|
||||
|
||||
## Vite Error
|
||||
|
||||
- If you receive an "Illuminate\Foundation\ViteException: Unable to locate file in Vite manifest" error, you can run `npm run build` or ask the user to run `npm run dev` or `composer run dev`.
|
||||
|
||||
=== laravel/v12 rules ===
|
||||
|
||||
# Laravel 12
|
||||
|
||||
- CRITICAL: ALWAYS use `search-docs` tool for version-specific Laravel documentation and updated code examples.
|
||||
- Since Laravel 11, Laravel has a new streamlined file structure which this project uses.
|
||||
|
||||
## Laravel 12 Structure
|
||||
|
||||
- In Laravel 12, middleware are no longer registered in `app/Http/Kernel.php`.
|
||||
- Middleware are configured declaratively in `bootstrap/app.php` using `Application::configure()->withMiddleware()`.
|
||||
- `bootstrap/app.php` is the file to register middleware, exceptions, and routing files.
|
||||
- `bootstrap/providers.php` contains application specific service providers.
|
||||
- The `app\Console\Kernel.php` file no longer exists; use `bootstrap/app.php` or `routes/console.php` for console configuration.
|
||||
- Console commands in `app/Console/Commands/` are automatically available and do not require manual registration.
|
||||
|
||||
## Database
|
||||
|
||||
- When modifying a column, the migration must include all of the attributes that were previously defined on the column. Otherwise, they will be dropped and lost.
|
||||
- Laravel 12 allows limiting eagerly loaded records natively, without external packages: `$query->latest()->limit(10);`.
|
||||
|
||||
### Models
|
||||
|
||||
- Casts can and likely should be set in a `casts()` method on a model rather than the `$casts` property. Follow existing conventions from other models.
|
||||
|
||||
=== pint/core rules ===
|
||||
|
||||
# Laravel Pint Code Formatter
|
||||
|
||||
- If you have modified any PHP files, you must run `vendor/bin/pint --dirty --format agent` before finalizing changes to ensure your code matches the project's expected style.
|
||||
- Do not run `vendor/bin/pint --test --format agent`, simply run `vendor/bin/pint --format agent` to fix any formatting issues.
|
||||
|
||||
=== phpunit/core rules ===
|
||||
|
||||
# PHPUnit
|
||||
|
||||
- This application uses PHPUnit for testing. All tests must be written as PHPUnit classes. Use `php artisan make:test --phpunit {name}` to create a new test.
|
||||
- If you see a test using "Pest", convert it to PHPUnit.
|
||||
- Every time a test has been updated, run that singular test.
|
||||
- When the tests relating to your feature are passing, ask the user if they would like to also run the entire test suite to make sure everything is still passing.
|
||||
- Tests should cover all happy paths, failure paths, and edge cases.
|
||||
- You must not remove any tests or test files from the tests directory without approval. These are not temporary or helper files; these are core to the application.
|
||||
|
||||
## Running Tests
|
||||
|
||||
- Run the minimal number of tests, using an appropriate filter, before finalizing.
|
||||
- To run all tests: `php artisan test --compact`.
|
||||
- To run all tests in a file: `php artisan test --compact tests/Feature/ExampleTest.php`.
|
||||
- To filter on a particular test name: `php artisan test --compact --filter=testName` (recommended after making a change to a related file).
|
||||
|
||||
=== tailwindcss/core rules ===
|
||||
|
||||
# Tailwind CSS
|
||||
|
||||
- Always use existing Tailwind conventions; check project patterns before adding new ones.
|
||||
- IMPORTANT: Always use `search-docs` tool for version-specific Tailwind CSS documentation and updated code examples. Never rely on training data.
|
||||
- IMPORTANT: Activate `tailwindcss-development` every time you're working with a Tailwind CSS or styling-related task.
|
||||
|
||||
=== filament/filament rules ===
|
||||
|
||||
## Filament
|
||||
|
||||
- Filament is used by this application. Follow the existing conventions for how and where it is implemented.
|
||||
- Filament is a Server-Driven UI (SDUI) framework for Laravel that lets you define user interfaces in PHP using structured configuration objects. Built on Livewire, Alpine.js, and Tailwind CSS.
|
||||
- Use the `search-docs` tool for official documentation on Artisan commands, code examples, testing, relationships, and idiomatic practices. If `search-docs` is unavailable, refer to https://filamentphp.com/docs.
|
||||
|
||||
### Artisan
|
||||
|
||||
- Always use Filament-specific Artisan commands to create files. Find available commands with the `list-artisan-commands` tool, or run `php artisan --help`.
|
||||
- Always inspect required options before running a command, and always pass `--no-interaction`.
|
||||
|
||||
### Patterns
|
||||
|
||||
Always use static `make()` methods to initialize components. Most configuration methods accept a `Closure` for dynamic values.
|
||||
|
||||
Use `Get $get` to read other form field values for conditional logic:
|
||||
|
||||
<code-snippet name="Conditional form field visibility" lang="php">
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
|
||||
Select::make('type')
|
||||
->options(CompanyType::class)
|
||||
->required()
|
||||
->live(),
|
||||
|
||||
TextInput::make('company_name')
|
||||
->required()
|
||||
->visible(fn (Get $get): bool => $get('type') === 'business'),
|
||||
|
||||
</code-snippet>
|
||||
|
||||
Use `state()` with a `Closure` to compute derived column values:
|
||||
|
||||
<code-snippet name="Computed table column value" lang="php">
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
|
||||
TextColumn::make('full_name')
|
||||
->state(fn (User $record): string => "{$record->first_name} {$record->last_name}"),
|
||||
|
||||
</code-snippet>
|
||||
|
||||
Actions encapsulate a button with an optional modal form and logic:
|
||||
|
||||
<code-snippet name="Action with modal form" lang="php">
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
|
||||
Action::make('updateEmail')
|
||||
->schema([
|
||||
TextInput::make('email')
|
||||
->email()
|
||||
->required(),
|
||||
])
|
||||
->action(fn (array $data, User $record) => $record->update($data))
|
||||
|
||||
</code-snippet>
|
||||
|
||||
### Testing
|
||||
|
||||
Always authenticate before testing panel functionality. Filament uses Livewire, so use `Livewire::test()` or `livewire()` (available when `pestphp/pest-plugin-livewire` is in `composer.json`):
|
||||
|
||||
<code-snippet name="Table test" lang="php">
|
||||
use function Pest\Livewire\livewire;
|
||||
|
||||
livewire(ListUsers::class)
|
||||
->assertCanSeeTableRecords($users)
|
||||
->searchTable($users->first()->name)
|
||||
->assertCanSeeTableRecords($users->take(1))
|
||||
->assertCanNotSeeTableRecords($users->skip(1));
|
||||
|
||||
</code-snippet>
|
||||
|
||||
<code-snippet name="Create resource test" lang="php">
|
||||
use function Pest\Laravel\assertDatabaseHas;
|
||||
use function Pest\Livewire\livewire;
|
||||
|
||||
livewire(CreateUser::class)
|
||||
->fillForm([
|
||||
'name' => 'Test',
|
||||
'email' => 'test@example.com',
|
||||
])
|
||||
->call('create')
|
||||
->assertNotified()
|
||||
->assertRedirect();
|
||||
|
||||
assertDatabaseHas(User::class, [
|
||||
'name' => 'Test',
|
||||
'email' => 'test@example.com',
|
||||
]);
|
||||
|
||||
</code-snippet>
|
||||
|
||||
<code-snippet name="Testing validation" lang="php">
|
||||
use function Pest\Livewire\livewire;
|
||||
|
||||
livewire(CreateUser::class)
|
||||
->fillForm([
|
||||
'name' => null,
|
||||
'email' => 'invalid-email',
|
||||
])
|
||||
->call('create')
|
||||
->assertHasFormErrors([
|
||||
'name' => 'required',
|
||||
'email' => 'email',
|
||||
])
|
||||
->assertNotNotified();
|
||||
|
||||
</code-snippet>
|
||||
|
||||
<code-snippet name="Calling actions in pages" lang="php">
|
||||
use Filament\Actions\DeleteAction;
|
||||
use function Pest\Livewire\livewire;
|
||||
|
||||
livewire(EditUser::class, ['record' => $user->id])
|
||||
->callAction(DeleteAction::class)
|
||||
->assertNotified()
|
||||
->assertRedirect();
|
||||
|
||||
</code-snippet>
|
||||
|
||||
<code-snippet name="Calling actions in tables" lang="php">
|
||||
use Filament\Actions\Testing\TestAction;
|
||||
use function Pest\Livewire\livewire;
|
||||
|
||||
livewire(ListUsers::class)
|
||||
->callAction(TestAction::make('promote')->table($user), [
|
||||
'role' => 'admin',
|
||||
])
|
||||
->assertNotified();
|
||||
|
||||
</code-snippet>
|
||||
|
||||
### Correct Namespaces
|
||||
|
||||
- Form fields (`TextInput`, `Select`, etc.): `Filament\Forms\Components\`
|
||||
- Infolist entries (`TextEntry`, `IconEntry`, etc.): `Filament\Infolists\Components\`
|
||||
- Layout components (`Grid`, `Section`, `Fieldset`, `Tabs`, `Wizard`, etc.): `Filament\Schemas\Components\`
|
||||
- Schema utilities (`Get`, `Set`, etc.): `Filament\Schemas\Components\Utilities\`
|
||||
- Actions (`DeleteAction`, `CreateAction`, etc.): `Filament\Actions\`. Never use `Filament\Tables\Actions\`, `Filament\Forms\Actions\`, or any other sub-namespace for actions.
|
||||
- Icons: `Filament\Support\Icons\Heroicon` enum (e.g., `Heroicon::PencilSquare`)
|
||||
|
||||
### Common Mistakes
|
||||
|
||||
- **Never assume public file visibility.** File visibility is `private` by default. Always use `->visibility('public')` when public access is needed.
|
||||
- **Never assume full-width layout.** `Grid`, `Section`, and `Fieldset` do not span all columns by default. Explicitly set column spans when needed.
|
||||
|
||||
=== laravel/ai rules ===
|
||||
|
||||
## Laravel AI SDK
|
||||
|
||||
- This application uses the Laravel AI SDK (`laravel/ai`) for all AI functionality.
|
||||
- Activate the `developing-with-ai-sdk` skill when building, editing, updating, debugging, or testing AI agents, text generation, chat, streaming, structured output, tools, image generation, audio, transcription, embeddings, reranking, vector stores, files, conversation memory, or any AI provider integration (OpenAI, Anthropic, Gemini, Cohere, Groq, xAI, ElevenLabs, Jina, OpenRouter).
|
||||
|
||||
</laravel-boost-guidelines>
|
||||
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::connection(config('activitylog.database_connection'))
|
||||
->create(config('activitylog.table_name'), function (Blueprint $table): void {
|
||||
$table->bigIncrements('id');
|
||||
$table->string('log_name')->nullable();
|
||||
$table->text('description');
|
||||
$table->nullableMorphs('subject', 'subject');
|
||||
$table->string('event')->nullable();
|
||||
$table->nullableMorphs('causer', 'causer');
|
||||
$table->json('properties')->nullable();
|
||||
$table->uuid('batch_uuid')->nullable();
|
||||
$table->timestamps();
|
||||
$table->index('log_name');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::connection(config('activitylog.database_connection'))
|
||||
->dropIfExists(config('activitylog.table_name'));
|
||||
}
|
||||
};
|
||||
@ -1,68 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Admin\Filament\Pages;
|
||||
|
||||
use App\Settings\GeneralSettings;
|
||||
use App\Support\HomeSlideDefaults;
|
||||
use BackedEnum;
|
||||
use Filament\Pages\SettingsPage;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Support\Enums\Width;
|
||||
use Modules\Admin\Support\HomeSlideFormSchema;
|
||||
use Modules\S3\Support\MediaStorage;
|
||||
use UnitEnum;
|
||||
|
||||
class ManageHomeSlides extends SettingsPage
|
||||
{
|
||||
protected static string $settings = GeneralSettings::class;
|
||||
|
||||
protected static ?string $title = 'Home Slides';
|
||||
|
||||
protected static ?string $navigationLabel = 'Home Slides';
|
||||
|
||||
protected static string | BackedEnum | null $navigationIcon = 'heroicon-o-photo';
|
||||
|
||||
protected static string | UnitEnum | null $navigationGroup = 'Content';
|
||||
|
||||
protected static ?int $navigationSort = 2;
|
||||
|
||||
protected Width | string | null $maxContentWidth = Width::Full;
|
||||
|
||||
protected function mutateFormDataBeforeFill(array $data): array
|
||||
{
|
||||
return [
|
||||
'home_slides' => $this->normalizeHomeSlides(
|
||||
$data['home_slides'] ?? $this->defaultHomeSlides(),
|
||||
MediaStorage::storedDisk('public'),
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
protected function mutateFormDataBeforeSave(array $data): array
|
||||
{
|
||||
$data['home_slides'] = $this->normalizeHomeSlides($data['home_slides'] ?? [], MediaStorage::activeDisk());
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
HomeSlideFormSchema::make(
|
||||
$this->defaultHomeSlides(),
|
||||
fn ($state): array => $this->normalizeHomeSlides($state, MediaStorage::activeDisk()),
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
private function defaultHomeSlides(): array
|
||||
{
|
||||
return HomeSlideDefaults::defaults();
|
||||
}
|
||||
|
||||
private function normalizeHomeSlides(mixed $state, ?string $defaultDisk = null): array
|
||||
{
|
||||
return HomeSlideDefaults::normalize($state, $defaultDisk);
|
||||
}
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources\CategoryResource\Pages;
|
||||
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Modules\Admin\Filament\Resources\CategoryResource;
|
||||
|
||||
class EditCategory extends EditRecord
|
||||
{
|
||||
protected static string $resource = CategoryResource::class;
|
||||
protected function getHeaderActions(): array { return [DeleteAction::make()]; }
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources\CityResource\Pages;
|
||||
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Modules\Admin\Filament\Resources\CityResource;
|
||||
|
||||
class EditCity extends EditRecord
|
||||
{
|
||||
protected static string $resource = CityResource::class;
|
||||
protected function getHeaderActions(): array { return [DeleteAction::make()]; }
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources\CityResource\Pages;
|
||||
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Modules\Admin\Filament\Resources\CityResource;
|
||||
|
||||
class ListCities extends ListRecords
|
||||
{
|
||||
protected static string $resource = CityResource::class;
|
||||
protected function getHeaderActions(): array { return [CreateAction::make()]; }
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources\DistrictResource\Pages;
|
||||
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Modules\Admin\Filament\Resources\DistrictResource;
|
||||
|
||||
class EditDistrict extends EditRecord
|
||||
{
|
||||
protected static string $resource = DistrictResource::class;
|
||||
protected function getHeaderActions(): array { return [DeleteAction::make()]; }
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources\DistrictResource\Pages;
|
||||
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Modules\Admin\Filament\Resources\DistrictResource;
|
||||
|
||||
class ListDistricts extends ListRecords
|
||||
{
|
||||
protected static string $resource = DistrictResource::class;
|
||||
protected function getHeaderActions(): array { return [CreateAction::make()]; }
|
||||
}
|
||||
@ -1,218 +0,0 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources;
|
||||
|
||||
use A909M\FilamentStateFusion\Forms\Components\StateFusionSelect;
|
||||
use A909M\FilamentStateFusion\Tables\Columns\StateFusionSelectColumn;
|
||||
use A909M\FilamentStateFusion\Tables\Filters\StateFusionSelectFilter;
|
||||
use App\Support\CountryCodeManager;
|
||||
use BackedEnum;
|
||||
use Cheesegrits\FilamentGoogleMaps\Fields\Map;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Forms\Components\DatePicker;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\SpatieMediaLibraryImageColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Enums\FiltersLayout;
|
||||
use Filament\Tables\Filters\Filter;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Filters\TernaryFilter;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Modules\Admin\Filament\Resources\ListingResource\Pages;
|
||||
use Modules\Category\Models\Category;
|
||||
use Modules\Listing\Models\Listing;
|
||||
use Modules\Listing\Support\ListingCustomFieldSchemaBuilder;
|
||||
use Modules\Listing\Support\ListingPanelHelper;
|
||||
use Modules\Location\Models\City;
|
||||
use Modules\Location\Models\Country;
|
||||
use Modules\Video\Support\Filament\VideoFormSchema;
|
||||
use UnitEnum;
|
||||
use Ysfkaya\FilamentPhoneInput\Forms\PhoneInput;
|
||||
|
||||
class ListingResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Listing::class;
|
||||
protected static string | BackedEnum | null $navigationIcon = 'heroicon-o-clipboard-document-list';
|
||||
protected static string | UnitEnum | null $navigationGroup = 'Catalog';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema->schema([
|
||||
TextInput::make('title')->required()->maxLength(255)->live(onBlur: true)->afterStateUpdated(fn ($state, $set) => $set('slug', \Illuminate\Support\Str::slug($state) . '-' . \Illuminate\Support\Str::random(4))),
|
||||
TextInput::make('slug')->required()->maxLength(255)->unique(ignoreRecord: true),
|
||||
Textarea::make('description')->rows(4),
|
||||
TextInput::make('price')
|
||||
->numeric()
|
||||
->currencyMask(thousandSeparator: ',', decimalSeparator: '.', precision: 2),
|
||||
Select::make('currency')
|
||||
->options(fn () => ListingPanelHelper::currencyOptions())
|
||||
->default(fn () => ListingPanelHelper::defaultCurrency())
|
||||
->required(),
|
||||
Select::make('category_id')
|
||||
->label('Category')
|
||||
->options(fn () => Category::where('is_active', true)->pluck('name', 'id'))
|
||||
->searchable()
|
||||
->live()
|
||||
->afterStateUpdated(fn ($state, $set) => $set('custom_fields', []))
|
||||
->nullable(),
|
||||
Select::make('user_id')->relationship('user', 'email')->label('Owner')->searchable()->preload()->nullable(),
|
||||
Section::make('Custom Fields')
|
||||
->description('Category specific listing attributes.')
|
||||
->schema(fn (Get $get): array => ListingCustomFieldSchemaBuilder::formComponents(
|
||||
($categoryId = $get('category_id')) ? (int) $categoryId : null
|
||||
))
|
||||
->columns(2)
|
||||
->columnSpanFull()
|
||||
->visible(fn (Get $get): bool => ListingCustomFieldSchemaBuilder::hasFields(
|
||||
($categoryId = $get('category_id')) ? (int) $categoryId : null
|
||||
)),
|
||||
StateFusionSelect::make('status')->required(),
|
||||
PhoneInput::make('contact_phone')->defaultCountry(CountryCodeManager::defaultCountryIso2())->nullable(),
|
||||
TextInput::make('contact_email')->email()->maxLength(255),
|
||||
Toggle::make('is_featured')->default(false),
|
||||
Select::make('country')
|
||||
->label('Country')
|
||||
->options(fn (): array => Country::query()
|
||||
->orderBy('name')
|
||||
->pluck('name', 'name')
|
||||
->all())
|
||||
->searchable()
|
||||
->preload()
|
||||
->live()
|
||||
->afterStateUpdated(fn ($state, $set) => $set('city', null))
|
||||
->nullable(),
|
||||
Select::make('city')
|
||||
->label('City')
|
||||
->options(function (Get $get): array {
|
||||
$country = $get('country');
|
||||
|
||||
return City::query()
|
||||
->where('is_active', true)
|
||||
->when($country, fn (Builder $query, string $country): Builder => $query->whereHas('country', fn (Builder $countryQuery): Builder => $countryQuery->where('name', $country)))
|
||||
->orderBy('name')
|
||||
->pluck('name', 'name')
|
||||
->all();
|
||||
})
|
||||
->searchable()
|
||||
->preload()
|
||||
->nullable(),
|
||||
Map::make('location')
|
||||
->label('Location')
|
||||
->visible(fn (): bool => ListingPanelHelper::googleMapsEnabled())
|
||||
->draggable()
|
||||
->clickable()
|
||||
->autocomplete('city')
|
||||
->autocompleteReverse(true)
|
||||
->reverseGeocode([
|
||||
'city' => '%L',
|
||||
])
|
||||
->defaultLocation([41.0082, 28.9784])
|
||||
->defaultZoom(10)
|
||||
->height('320px')
|
||||
->columnSpanFull(),
|
||||
SpatieMediaLibraryFileUpload::make('images')
|
||||
->collection('listing-images')
|
||||
->multiple()
|
||||
->image()
|
||||
->reorderable(),
|
||||
VideoFormSchema::listingSection(),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table->columns([
|
||||
SpatieMediaLibraryImageColumn::make('images')
|
||||
->collection('listing-images')
|
||||
->circular(),
|
||||
TextColumn::make('id')->sortable(),
|
||||
TextColumn::make('title')->searchable()->sortable()->limit(40),
|
||||
TextColumn::make('category.name')->label('Category')->sortable(),
|
||||
TextColumn::make('user.email')->label('Owner')->searchable()->toggleable()->sortable(),
|
||||
TextColumn::make('price')
|
||||
->currency(fn (Listing $record): string => $record->currency ?: ListingPanelHelper::defaultCurrency())
|
||||
->sortable(),
|
||||
StateFusionSelectColumn::make('status')->sortable(),
|
||||
IconColumn::make('is_featured')->boolean()->label('Featured')->sortable(),
|
||||
TextColumn::make('city')->sortable(),
|
||||
TextColumn::make('country')->sortable(),
|
||||
TextColumn::make('created_at')->dateTime()->sortable(),
|
||||
])->filters([
|
||||
StateFusionSelectFilter::make('status'),
|
||||
SelectFilter::make('category_id')
|
||||
->label('Category')
|
||||
->relationship('category', 'name')
|
||||
->searchable()
|
||||
->preload(),
|
||||
SelectFilter::make('user_id')
|
||||
->label('Owner')
|
||||
->relationship('user', 'email')
|
||||
->searchable()
|
||||
->preload(),
|
||||
SelectFilter::make('country')
|
||||
->options(fn (): array => Country::query()
|
||||
->orderBy('name')
|
||||
->pluck('name', 'name')
|
||||
->all())
|
||||
->searchable(),
|
||||
SelectFilter::make('city')
|
||||
->options(fn (): array => City::query()
|
||||
->orderBy('name')
|
||||
->pluck('name', 'name')
|
||||
->all())
|
||||
->searchable(),
|
||||
TernaryFilter::make('is_featured')->label('Featured'),
|
||||
Filter::make('created_at')
|
||||
->label('Created Date')
|
||||
->schema([
|
||||
DatePicker::make('from')->label('From'),
|
||||
DatePicker::make('until')->label('Until'),
|
||||
])
|
||||
->query(fn (Builder $query, array $data): Builder => $query
|
||||
->when($data['from'] ?? null, fn (Builder $query, string $date): Builder => $query->whereDate('created_at', '>=', $date))
|
||||
->when($data['until'] ?? null, fn (Builder $query, string $date): Builder => $query->whereDate('created_at', '<=', $date))),
|
||||
Filter::make('price')
|
||||
->label('Price Range')
|
||||
->schema([
|
||||
TextInput::make('min')->numeric()->label('Min'),
|
||||
TextInput::make('max')->numeric()->label('Max'),
|
||||
])
|
||||
->query(fn (Builder $query, array $data): Builder => $query
|
||||
->when($data['min'] ?? null, fn (Builder $query, string $amount): Builder => $query->where('price', '>=', (float) $amount))
|
||||
->when($data['max'] ?? null, fn (Builder $query, string $amount): Builder => $query->where('price', '<=', (float) $amount))),
|
||||
])
|
||||
->filtersLayout(FiltersLayout::AboveContent)
|
||||
->filtersFormColumns(3)
|
||||
->filtersFormWidth('7xl')
|
||||
->persistFiltersInSession()
|
||||
->defaultSort('id', 'desc')
|
||||
->actions([
|
||||
EditAction::make(),
|
||||
Action::make('activities')
|
||||
->icon('heroicon-o-clock')
|
||||
->url(fn (Listing $record): string => static::getUrl('activities', ['record' => $record])),
|
||||
DeleteAction::make(),
|
||||
]);
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListListings::route('/'),
|
||||
'create' => Pages\CreateListing::route('/create'),
|
||||
'activities' => Pages\ListListingActivities::route('/{record}/activities'),
|
||||
'edit' => Pages\EditListing::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources\ListingResource\Pages;
|
||||
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Modules\Admin\Filament\Resources\ListingResource;
|
||||
|
||||
class EditListing extends EditRecord
|
||||
{
|
||||
protected static string $resource = ListingResource::class;
|
||||
protected function getHeaderActions(): array { return [DeleteAction::make()]; }
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources\ListingResource\Pages;
|
||||
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Modules\Admin\Filament\Resources\ListingResource;
|
||||
|
||||
class ListListings extends ListRecords
|
||||
{
|
||||
protected static string $resource = ListingResource::class;
|
||||
protected function getHeaderActions(): array { return [CreateAction::make()]; }
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources\LocationResource\Pages;
|
||||
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Modules\Admin\Filament\Resources\LocationResource;
|
||||
|
||||
class CreateLocation extends CreateRecord
|
||||
{
|
||||
protected static string $resource = LocationResource::class;
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources\LocationResource\Pages;
|
||||
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Modules\Admin\Filament\Resources\LocationResource;
|
||||
|
||||
class EditLocation extends EditRecord
|
||||
{
|
||||
protected static string $resource = LocationResource::class;
|
||||
protected function getHeaderActions(): array { return [DeleteAction::make()]; }
|
||||
}
|
||||
@ -1,10 +0,0 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources\LocationResource\Pages;
|
||||
|
||||
use Modules\Admin\Filament\Resources\LocationResource;
|
||||
use pxlrbt\FilamentActivityLog\Pages\ListActivities;
|
||||
|
||||
class ListLocationActivities extends ListActivities
|
||||
{
|
||||
protected static string $resource = LocationResource::class;
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources\LocationResource\Pages;
|
||||
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Modules\Admin\Filament\Resources\LocationResource;
|
||||
|
||||
class ListLocations extends ListRecords
|
||||
{
|
||||
protected static string $resource = LocationResource::class;
|
||||
protected function getHeaderActions(): array { return [CreateAction::make()]; }
|
||||
}
|
||||
@ -1,12 +0,0 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources\UserResource\Pages;
|
||||
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Modules\Admin\Filament\Resources\UserResource;
|
||||
|
||||
class ListUsers extends ListRecords
|
||||
{
|
||||
protected static string $resource = UserResource::class;
|
||||
protected function getHeaderActions(): array { return [CreateAction::make()]; }
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Admin\Providers;
|
||||
|
||||
use App\Http\Middleware\BootstrapAppData;
|
||||
use A909M\FilamentStateFusion\FilamentStateFusionPlugin;
|
||||
use DutchCodingCompany\FilamentDeveloperLogins\FilamentDeveloperLoginsPlugin;
|
||||
use Filament\Http\Middleware\Authenticate;
|
||||
@ -13,7 +13,6 @@ use Filament\Pages\Dashboard;
|
||||
use Filament\Panel;
|
||||
use Filament\PanelProvider;
|
||||
use Filament\Support\Colors\Color;
|
||||
use Filament\View\PanelsRenderHook;
|
||||
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
|
||||
use Illuminate\Cookie\Middleware\EncryptCookies;
|
||||
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
|
||||
@ -21,13 +20,16 @@ use Illuminate\Routing\Middleware\SubstituteBindings;
|
||||
use Illuminate\Session\Middleware\StartSession;
|
||||
use Illuminate\View\Middleware\ShareErrorsFromSession;
|
||||
use Jeffgreco13\FilamentBreezy\BreezyCore;
|
||||
use MWGuerra\FileManager\FileManagerPlugin;
|
||||
use MWGuerra\FileManager\Filament\Pages\FileManager;
|
||||
use Modules\Category\CategoryPlugin;
|
||||
use Modules\Demo\App\Http\Middleware\ResolveDemoRequest;
|
||||
use Modules\Admin\Filament\Resources\CategoryResource;
|
||||
use Modules\Admin\Filament\Resources\ListingResource;
|
||||
use Modules\Admin\Filament\Resources\LocationResource;
|
||||
use Modules\Admin\Filament\Resources\UserResource;
|
||||
use Modules\Listing\ListingPlugin;
|
||||
use Modules\Location\LocationPlugin;
|
||||
use Modules\Site\App\Http\Middleware\BootstrapAppData;
|
||||
use Modules\Site\SitePlugin;
|
||||
use Modules\User\UserPlugin;
|
||||
use Modules\Video\VideoPlugin;
|
||||
use MWGuerra\FileManager\Filament\Pages\FileManager;
|
||||
use MWGuerra\FileManager\FileManagerPlugin;
|
||||
|
||||
class AdminPanelProvider extends PanelProvider
|
||||
{
|
||||
@ -39,11 +41,6 @@ class AdminPanelProvider extends PanelProvider
|
||||
->path('admin')
|
||||
->login()
|
||||
->colors(['primary' => Color::Blue])
|
||||
->discoverResources(in: module_path('Admin', 'Filament/Resources'), for: 'Modules\\Admin\\Filament\\Resources')
|
||||
->discoverResources(in: module_path('Video', 'Filament/Admin/Resources'), for: 'Modules\\Video\\Filament\\Admin\\Resources')
|
||||
->discoverPages(in: module_path('Admin', 'Filament/Pages'), for: 'Modules\\Admin\\Filament\\Pages')
|
||||
->discoverWidgets(in: module_path('Admin', 'Filament/Widgets'), for: 'Modules\\Admin\\Filament\\Widgets')
|
||||
->renderHook(PanelsRenderHook::BODY_END, fn () => view('video::partials.video-upload-optimizer'))
|
||||
->userMenuItems([
|
||||
'view-site' => MenuItem::make()
|
||||
->label('View Site')
|
||||
@ -70,6 +67,12 @@ class AdminPanelProvider extends PanelProvider
|
||||
->users([
|
||||
'Admin' => 'a@a.com',
|
||||
]),
|
||||
CategoryPlugin::make(),
|
||||
ListingPlugin::make(),
|
||||
LocationPlugin::make(),
|
||||
SitePlugin::make(),
|
||||
UserPlugin::make(),
|
||||
VideoPlugin::make(),
|
||||
])
|
||||
->pages([Dashboard::class])
|
||||
->middleware([
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Admin\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
@ -7,11 +8,9 @@ class AdminServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function boot(): void
|
||||
{
|
||||
$this->loadMigrationsFrom(module_path('Admin', 'database/migrations'));
|
||||
$this->loadMigrationsFrom(module_path('Admin', 'Database/migrations'));
|
||||
}
|
||||
|
||||
public function register(): void
|
||||
{
|
||||
$this->app->register(AdminPanelProvider::class);
|
||||
}
|
||||
{}
|
||||
}
|
||||
|
||||
35
Modules/Admin/Support/Filament/ResourceTableActions.php
Normal file
35
Modules/Admin/Support/Filament/ResourceTableActions.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Admin\Support\Filament;
|
||||
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\EditAction;
|
||||
|
||||
final class ResourceTableActions
|
||||
{
|
||||
public static function editDelete(): array
|
||||
{
|
||||
return [
|
||||
EditAction::make(),
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function editActivityDelete(string $resourceClass, array $afterActivity = []): array
|
||||
{
|
||||
return [
|
||||
EditAction::make(),
|
||||
self::activities($resourceClass),
|
||||
...$afterActivity,
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function activities(string $resourceClass): Action
|
||||
{
|
||||
return Action::make('activities')
|
||||
->icon('heroicon-o-clock')
|
||||
->url(fn ($record): string => $resourceClass::getUrl('activities', ['record' => $record]));
|
||||
}
|
||||
}
|
||||
27
Modules/Admin/Support/Filament/ResourceTableColumns.php
Normal file
27
Modules/Admin/Support/Filament/ResourceTableColumns.php
Normal file
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Admin\Support\Filament;
|
||||
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
|
||||
final class ResourceTableColumns
|
||||
{
|
||||
public static function id(string $name = 'id'): TextColumn
|
||||
{
|
||||
return TextColumn::make($name)->sortable();
|
||||
}
|
||||
|
||||
public static function activeIcon(string $name = 'is_active', string $label = 'Active'): IconColumn
|
||||
{
|
||||
return IconColumn::make($name)->label($label)->boolean();
|
||||
}
|
||||
|
||||
public static function createdAtHidden(string $name = 'created_at'): TextColumn
|
||||
{
|
||||
return TextColumn::make($name)
|
||||
->dateTime()
|
||||
->sortable()
|
||||
->toggleable(isToggledHiddenByDefault: true);
|
||||
}
|
||||
}
|
||||
29
Modules/Category/CategoryPlugin.php
Normal file
29
Modules/Category/CategoryPlugin.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Category;
|
||||
|
||||
use Filament\Contracts\Plugin;
|
||||
use Filament\Panel;
|
||||
|
||||
final class CategoryPlugin implements Plugin
|
||||
{
|
||||
public function getId(): string
|
||||
{
|
||||
return 'category';
|
||||
}
|
||||
|
||||
public static function make(): static
|
||||
{
|
||||
return app(static::class);
|
||||
}
|
||||
|
||||
public function register(Panel $panel): void
|
||||
{
|
||||
$panel->discoverResources(
|
||||
in: module_path('Category', 'Filament/Admin/Resources'),
|
||||
for: 'Modules\\Category\\Filament\\Admin\\Resources',
|
||||
);
|
||||
}
|
||||
|
||||
public function boot(Panel $panel): void {}
|
||||
}
|
||||
@ -9,25 +9,25 @@ class CategorySeeder extends Seeder
|
||||
public function run(): void
|
||||
{
|
||||
$categories = [
|
||||
['name' => 'Electronics', 'slug' => 'electronics', 'icon' => 'laptop', 'children' => ['Phones', 'Computers', 'Tablets', 'TVs']],
|
||||
['name' => 'Vehicles', 'slug' => 'vehicles', 'icon' => 'car', 'children' => ['Cars', 'Motorcycles', 'Trucks', 'Boats']],
|
||||
['name' => 'Real Estate', 'slug' => 'real-estate', 'icon' => 'home', 'children' => ['For Sale', 'For Rent', 'Commercial']],
|
||||
['name' => 'Fashion', 'slug' => 'fashion', 'icon' => 'shirt', 'children' => ['Men', 'Women', 'Kids', 'Shoes']],
|
||||
['name' => 'Home & Garden', 'slug' => 'home-garden', 'icon' => 'sofa', 'children' => ['Furniture', 'Garden', 'Appliances']],
|
||||
['name' => 'Sports', 'slug' => 'sports', 'icon' => 'football', 'children' => ['Outdoor', 'Fitness', 'Team Sports']],
|
||||
['name' => 'Jobs', 'slug' => 'jobs', 'icon' => 'briefcase', 'children' => ['Full Time', 'Part Time', 'Freelance']],
|
||||
['name' => 'Services', 'slug' => 'services', 'icon' => 'wrench', 'children' => ['Cleaning', 'Repair', 'Education']],
|
||||
['name' => 'Electronics', 'slug' => 'electronics', 'icon' => 'img/category/electronics.png', 'children' => ['Phones', 'Computers', 'Tablets', 'TVs']],
|
||||
['name' => 'Vehicles', 'slug' => 'vehicles', 'icon' => 'img/category/car.png', 'children' => ['Cars', 'Motorcycles', 'Trucks', 'Boats']],
|
||||
['name' => 'Real Estate', 'slug' => 'real-estate', 'icon' => 'img/category/home_garden.png', 'children' => ['For Sale', 'For Rent', 'Commercial']],
|
||||
['name' => 'Fashion', 'slug' => 'fashion', 'icon' => 'img/category/phone.png', 'children' => ['Men', 'Women', 'Kids', 'Shoes']],
|
||||
['name' => 'Home', 'slug' => 'home-garden', 'icon' => 'img/category/home_tools.png', 'children' => ['Furniture', 'Garden', 'Appliances']],
|
||||
['name' => 'Sports', 'slug' => 'sports', 'icon' => 'img/category/sports.png', 'children' => ['Outdoor', 'Fitness', 'Team Sports']],
|
||||
['name' => 'Jobs', 'slug' => 'jobs', 'icon' => 'img/category/education.png', 'children' => ['Full Time', 'Part Time', 'Freelance']],
|
||||
['name' => 'Services', 'slug' => 'services', 'icon' => 'img/category/home_tools.png', 'children' => ['Cleaning', 'Repair', 'Education']],
|
||||
];
|
||||
|
||||
foreach ($categories as $index => $data) {
|
||||
$parent = Category::firstOrCreate(
|
||||
$parent = Category::updateOrCreate(
|
||||
['slug' => $data['slug']],
|
||||
['name' => $data['name'], 'slug' => $data['slug'], 'icon' => $data['icon'], 'level' => 0, 'sort_order' => $index, 'is_active' => true]
|
||||
);
|
||||
|
||||
foreach ($data['children'] as $i => $childName) {
|
||||
$childSlug = $data['slug'] . '-' . \Illuminate\Support\Str::slug($childName);
|
||||
Category::firstOrCreate(
|
||||
Category::updateOrCreate(
|
||||
['slug' => $childSlug],
|
||||
['name' => $childName, 'slug' => $childSlug, 'parent_id' => $parent->id, 'level' => 1, 'sort_order' => $i, 'is_active' => true]
|
||||
);
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
@ -1,27 +1,29 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources;
|
||||
|
||||
namespace Modules\Category\Filament\Admin\Resources;
|
||||
|
||||
use BackedEnum;
|
||||
use Filament\Actions\Action;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
use Modules\Admin\Filament\Resources\CategoryResource\Pages;
|
||||
use Modules\Admin\Support\Filament\ResourceTableActions;
|
||||
use Modules\Admin\Support\Filament\ResourceTableColumns;
|
||||
use Modules\Category\Models\Category;
|
||||
use Modules\Category\Filament\Admin\Resources\CategoryResource\Pages;
|
||||
use UnitEnum;
|
||||
|
||||
class CategoryResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Category::class;
|
||||
protected static string | BackedEnum | null $navigationIcon = 'heroicon-o-tag';
|
||||
protected static string | UnitEnum | null $navigationGroup = 'Catalog';
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-tag';
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Catalog';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
@ -30,7 +32,7 @@ class CategoryResource extends Resource
|
||||
TextInput::make('slug')->required()->maxLength(255)->unique(ignoreRecord: true),
|
||||
TextInput::make('description')->maxLength(500),
|
||||
TextInput::make('icon')->maxLength(100),
|
||||
Select::make('parent_id')->label('Parent Category')->options(fn () => Category::whereNull('parent_id')->pluck('name', 'id'))->nullable()->searchable(),
|
||||
Select::make('parent_id')->label('Parent Category')->options(fn (): array => Category::rootIdNameOptions())->nullable()->searchable(),
|
||||
TextInput::make('sort_order')->numeric()->default(0),
|
||||
Toggle::make('is_active')->default(true),
|
||||
]);
|
||||
@ -39,15 +41,15 @@ class CategoryResource extends Resource
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return $table->columns([
|
||||
TextColumn::make('id')->sortable(),
|
||||
ResourceTableColumns::id(),
|
||||
TextColumn::make('name')
|
||||
->searchable()
|
||||
->formatStateUsing(fn (string $state, Category $record): string => $record->parent_id === null ? $state : '↳ ' . $state)
|
||||
->formatStateUsing(fn (string $state, Category $record): string => $record->parent_id === null ? $state : '↳ '.$state)
|
||||
->weight(fn (Category $record): string => $record->parent_id === null ? 'semi-bold' : 'normal'),
|
||||
TextColumn::make('parent.name')->label('Parent')->default('-'),
|
||||
TextColumn::make('children_count')->label('Subcategories'),
|
||||
TextColumn::make('listings_count')->label('Listings'),
|
||||
IconColumn::make('is_active')->boolean(),
|
||||
ResourceTableColumns::activeIcon(),
|
||||
TextColumn::make('sort_order')->sortable(),
|
||||
])->actions([
|
||||
Action::make('toggleChildren')
|
||||
@ -55,11 +57,7 @@ class CategoryResource extends Resource
|
||||
->icon(fn (Category $record, Pages\ListCategories $livewire): string => $livewire->hasExpandedChildren($record) ? 'heroicon-o-chevron-down' : 'heroicon-o-chevron-right')
|
||||
->action(fn (Category $record, Pages\ListCategories $livewire) => $livewire->toggleChildren($record))
|
||||
->visible(fn (Category $record): bool => $record->parent_id === null && $record->children_count > 0),
|
||||
EditAction::make(),
|
||||
Action::make('activities')
|
||||
->icon('heroicon-o-clock')
|
||||
->url(fn (Category $record): string => static::getUrl('activities', ['record' => $record])),
|
||||
DeleteAction::make(),
|
||||
...ResourceTableActions::editActivityDelete(static::class),
|
||||
]);
|
||||
}
|
||||
|
||||
@ -1,8 +1,9 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources\CategoryResource\Pages;
|
||||
|
||||
namespace Modules\Category\Filament\Admin\Resources\CategoryResource\Pages;
|
||||
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Modules\Admin\Filament\Resources\CategoryResource;
|
||||
use Modules\Category\Filament\Admin\Resources\CategoryResource;
|
||||
|
||||
class CreateCategory extends CreateRecord
|
||||
{
|
||||
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Category\Filament\Admin\Resources\CategoryResource\Pages;
|
||||
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Modules\Category\Filament\Admin\Resources\CategoryResource;
|
||||
|
||||
class EditCategory extends EditRecord
|
||||
{
|
||||
protected static string $resource = CategoryResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [DeleteAction::make()];
|
||||
}
|
||||
}
|
||||
@ -1,11 +1,12 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources\CategoryResource\Pages;
|
||||
|
||||
namespace Modules\Category\Filament\Admin\Resources\CategoryResource\Pages;
|
||||
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Livewire\Attributes\Url;
|
||||
use Modules\Admin\Filament\Resources\CategoryResource;
|
||||
use Modules\Category\Filament\Admin\Resources\CategoryResource;
|
||||
use Modules\Category\Models\Category;
|
||||
|
||||
class ListCategories extends ListRecords
|
||||
@ -1,7 +1,8 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources\CategoryResource\Pages;
|
||||
|
||||
use Modules\Admin\Filament\Resources\CategoryResource;
|
||||
namespace Modules\Category\Filament\Admin\Resources\CategoryResource\Pages;
|
||||
|
||||
use Modules\Category\Filament\Admin\Resources\CategoryResource;
|
||||
use pxlrbt\FilamentActivityLog\Pages\ListActivities;
|
||||
|
||||
class ListCategoryActivities extends ListActivities
|
||||
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Category\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
@ -14,7 +15,25 @@ class Category extends Model
|
||||
{
|
||||
use LogsActivity;
|
||||
|
||||
private const ICON_PATHS = [
|
||||
'car' => 'img/category/car.png',
|
||||
'education' => 'img/category/education.png',
|
||||
'electronics' => 'img/category/electronics.png',
|
||||
'football' => 'img/category/sports.png',
|
||||
'home' => 'img/category/home_garden.png',
|
||||
'home-garden' => 'img/category/home_garden.png',
|
||||
'home_garden' => 'img/category/home_garden.png',
|
||||
'home-tools' => 'img/category/home_tools.png',
|
||||
'home_tools' => 'img/category/home_tools.png',
|
||||
'laptop' => 'img/category/laptop.png',
|
||||
'mobile' => 'img/category/phone.png',
|
||||
'pet' => 'img/category/pet.png',
|
||||
'phone' => 'img/category/phone.png',
|
||||
'sports' => 'img/category/sports.png',
|
||||
];
|
||||
|
||||
protected $fillable = ['name', 'slug', 'description', 'icon', 'parent_id', 'level', 'sort_order', 'is_active'];
|
||||
|
||||
protected $casts = ['is_active' => 'boolean'];
|
||||
|
||||
public function getActivitylogOptions(): LogOptions
|
||||
@ -86,6 +105,91 @@ class Category extends Model
|
||||
->get(['id', 'name']);
|
||||
}
|
||||
|
||||
public static function activeIdNameOptions(): array
|
||||
{
|
||||
return static::query()
|
||||
->active()
|
||||
->ordered()
|
||||
->pluck('name', 'id')
|
||||
->all();
|
||||
}
|
||||
|
||||
public static function activeCount(): int
|
||||
{
|
||||
return (int) static::query()
|
||||
->active()
|
||||
->count();
|
||||
}
|
||||
|
||||
public static function homeParentCategories(int $limit = 8): Collection
|
||||
{
|
||||
return static::query()
|
||||
->active()
|
||||
->whereNull('parent_id')
|
||||
->ordered()
|
||||
->limit($limit)
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function headerNavigationItems(int $limit = 8): array
|
||||
{
|
||||
return static::query()
|
||||
->active()
|
||||
->whereNull('parent_id')
|
||||
->ordered()
|
||||
->limit($limit)
|
||||
->get(['id', 'name', 'icon'])
|
||||
->map(fn (self $category): array => [
|
||||
'id' => (int) $category->id,
|
||||
'name' => (string) $category->name,
|
||||
'icon_url' => $category->iconUrl(),
|
||||
])
|
||||
->all();
|
||||
}
|
||||
|
||||
public static function activeAiCatalog(): Collection
|
||||
{
|
||||
return static::query()
|
||||
->active()
|
||||
->ordered()
|
||||
->get(['id', 'name', 'parent_id']);
|
||||
}
|
||||
|
||||
public static function panelQuickCatalog(): array
|
||||
{
|
||||
$all = static::query()
|
||||
->active()
|
||||
->ordered()
|
||||
->get(['id', 'name', 'parent_id', 'icon']);
|
||||
|
||||
$childrenCount = static::query()
|
||||
->active()
|
||||
->selectRaw('parent_id, count(*) as aggregate')
|
||||
->whereNotNull('parent_id')
|
||||
->groupBy('parent_id')
|
||||
->pluck('aggregate', 'parent_id');
|
||||
|
||||
return $all
|
||||
->map(fn (self $category): array => [
|
||||
'id' => (int) $category->id,
|
||||
'name' => (string) $category->name,
|
||||
'parent_id' => $category->parent_id ? (int) $category->parent_id : null,
|
||||
'icon' => $category->icon,
|
||||
'has_children' => ((int) ($childrenCount[$category->id] ?? 0)) > 0,
|
||||
])
|
||||
->all();
|
||||
}
|
||||
|
||||
public static function rootIdNameOptions(): array
|
||||
{
|
||||
return static::query()
|
||||
->active()
|
||||
->whereNull('parent_id')
|
||||
->ordered()
|
||||
->pluck('name', 'id')
|
||||
->all();
|
||||
}
|
||||
|
||||
public static function themePills(int $limit = 8): Collection
|
||||
{
|
||||
return static::query()
|
||||
@ -219,6 +323,32 @@ class Category extends Model
|
||||
return $this->hasMany(\Modules\Listing\Models\Listing::class)->where('status', 'active');
|
||||
}
|
||||
|
||||
public function resolvedIconPath(): ?string
|
||||
{
|
||||
$icon = trim((string) $this->icon);
|
||||
|
||||
if ($icon === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (isset(self::ICON_PATHS[$icon])) {
|
||||
return self::ICON_PATHS[$icon];
|
||||
}
|
||||
|
||||
if (preg_match('/\.(png|jpg|jpeg|webp|svg)$/i', $icon) === 1) {
|
||||
return ltrim($icon, '/');
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function iconUrl(): ?string
|
||||
{
|
||||
$path = $this->resolvedIconPath();
|
||||
|
||||
return $path ? asset($path) : null;
|
||||
}
|
||||
|
||||
private static function buildListingDirectoryTree(Collection $categories, Collection $activeListingCounts, ?int $parentId = null): Collection
|
||||
{
|
||||
return $categories
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Category\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
@ -9,10 +10,11 @@ class CategoryServiceProvider extends ServiceProvider
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
$this->loadMigrationsFrom(module_path($this->moduleName, 'database/migrations'));
|
||||
$this->loadMigrationsFrom(module_path($this->moduleName, 'Database/migrations'));
|
||||
$this->loadRoutesFrom(module_path($this->moduleName, 'routes/web.php'));
|
||||
$this->loadViewsFrom(module_path($this->moduleName, 'resources/views'), 'category');
|
||||
}
|
||||
|
||||
public function register(): void {}
|
||||
public function register(): void
|
||||
{}
|
||||
}
|
||||
|
||||
@ -1,37 +0,0 @@
|
||||
<?php
|
||||
namespace Modules\Category\Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use Modules\Category\Models\Category;
|
||||
|
||||
class CategorySeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$categories = [
|
||||
['name' => 'Electronics', 'slug' => 'electronics', 'icon' => 'laptop', 'children' => ['Phones', 'Computers', 'Tablets', 'TVs']],
|
||||
['name' => 'Vehicles', 'slug' => 'vehicles', 'icon' => 'car', 'children' => ['Cars', 'Motorcycles', 'Trucks', 'Boats']],
|
||||
['name' => 'Real Estate', 'slug' => 'real-estate', 'icon' => 'home', 'children' => ['For Sale', 'For Rent', 'Commercial']],
|
||||
['name' => 'Fashion', 'slug' => 'fashion', 'icon' => 'shirt', 'children' => ['Men', 'Women', 'Kids', 'Shoes']],
|
||||
['name' => 'Home & Garden', 'slug' => 'home-garden', 'icon' => 'sofa', 'children' => ['Furniture', 'Garden', 'Appliances']],
|
||||
['name' => 'Sports', 'slug' => 'sports', 'icon' => 'football', 'children' => ['Outdoor', 'Fitness', 'Team Sports']],
|
||||
['name' => 'Jobs', 'slug' => 'jobs', 'icon' => 'briefcase', 'children' => ['Full Time', 'Part Time', 'Freelance']],
|
||||
['name' => 'Services', 'slug' => 'services', 'icon' => 'wrench', 'children' => ['Cleaning', 'Repair', 'Education']],
|
||||
];
|
||||
|
||||
foreach ($categories as $index => $data) {
|
||||
$parent = Category::firstOrCreate(
|
||||
['slug' => $data['slug']],
|
||||
['name' => $data['name'], 'slug' => $data['slug'], 'icon' => $data['icon'], 'level' => 0, 'sort_order' => $index, 'is_active' => true]
|
||||
);
|
||||
|
||||
foreach ($data['children'] as $i => $childName) {
|
||||
$childSlug = $data['slug'] . '-' . \Illuminate\Support\Str::slug($childName);
|
||||
Category::firstOrCreate(
|
||||
['slug' => $childSlug],
|
||||
['name' => $childName, 'slug' => $childSlug, 'parent_id' => $parent->id, 'level' => 1, 'sort_order' => $i, 'is_active' => true]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,48 +1,8 @@
|
||||
@php
|
||||
$categoryCount = $categories->count();
|
||||
$subcategoryCount = $categories->sum(fn ($category) => $category->children->count());
|
||||
@endphp
|
||||
|
||||
<div class="max-w-[1320px] mx-auto px-4 py-5 md:py-7 space-y-7">
|
||||
<section class="overflow-hidden rounded-[28px] border border-slate-200/80 bg-white shadow-sm">
|
||||
<div class="grid gap-8 px-6 py-8 md:px-10 md:py-10 lg:grid-cols-[1.2fr,0.8fr] lg:items-end">
|
||||
<div class="space-y-5">
|
||||
<span class="inline-flex items-center rounded-full border border-blue-200 bg-blue-50 px-4 py-1.5 text-xs font-semibold uppercase tracking-[0.24em] text-blue-700">
|
||||
Browse categories
|
||||
</span>
|
||||
<div class="space-y-3">
|
||||
<h1 class="max-w-3xl text-3xl font-extrabold tracking-tight text-slate-950 md:text-5xl">
|
||||
Find the right marketplace section without leaving the same frontend shell.
|
||||
</h1>
|
||||
<p class="max-w-2xl text-base leading-7 text-slate-600 md:text-lg">
|
||||
Explore every top-level category from one clean directory. Header, footer, spacing, and navigation now stay aligned with the rest of the site.
|
||||
</p>
|
||||
</div>
|
||||
<div class="flex flex-wrap gap-3">
|
||||
<a href="{{ route('listings.index') }}" class="inline-flex min-h-12 items-center justify-center rounded-full bg-blue-600 px-6 text-sm font-semibold text-white shadow-sm transition hover:bg-blue-700">
|
||||
Browse all listings
|
||||
</a>
|
||||
<a href="{{ route('home') }}" class="inline-flex min-h-12 items-center justify-center rounded-full border border-slate-200 bg-white px-6 text-sm font-semibold text-slate-700 transition hover:border-slate-300 hover:text-slate-950">
|
||||
Go home
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="grid gap-4 sm:grid-cols-2">
|
||||
<div class="rounded-[24px] border border-slate-200 bg-slate-50 p-5">
|
||||
<p class="text-sm font-medium text-slate-500">Root categories</p>
|
||||
<p class="mt-3 text-3xl font-extrabold tracking-tight text-slate-950">{{ number_format($categoryCount, 0, '.', ',') }}</p>
|
||||
<p class="mt-2 text-sm text-slate-600">Only top-level sections are shown first for a simpler directory.</p>
|
||||
</div>
|
||||
<div class="rounded-[24px] border border-slate-200 bg-slate-50 p-5">
|
||||
<p class="text-sm font-medium text-slate-500">Subcategories</p>
|
||||
<p class="mt-3 text-3xl font-extrabold tracking-tight text-slate-950">{{ number_format($subcategoryCount, 0, '.', ',') }}</p>
|
||||
<p class="mt-2 text-sm text-slate-600">Each card previews its most relevant child sections before you drill in.</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="space-y-4">
|
||||
<div class="flex flex-col gap-2 sm:flex-row sm:items-end sm:justify-between">
|
||||
<div>
|
||||
@ -61,17 +21,7 @@
|
||||
->filter()
|
||||
->implode(' · ');
|
||||
$extraChildCount = max($category->children->count() - 3, 0);
|
||||
$icon = match (trim((string) ($category->icon ?? ''))) {
|
||||
'laptop' => 'heroicon-o-computer-desktop',
|
||||
'car' => 'heroicon-o-truck',
|
||||
'home' => 'heroicon-o-home',
|
||||
'shirt' => 'heroicon-o-shopping-bag',
|
||||
'sofa' => 'heroicon-o-home-modern',
|
||||
'football' => 'heroicon-o-trophy',
|
||||
'briefcase' => 'heroicon-o-briefcase',
|
||||
'wrench' => 'heroicon-o-wrench-screwdriver',
|
||||
default => null,
|
||||
};
|
||||
$iconUrl = $category->iconUrl();
|
||||
$iconLabel = strtoupper(\Illuminate\Support\Str::substr($category->name, 0, 1));
|
||||
@endphp
|
||||
<a
|
||||
@ -79,11 +29,11 @@
|
||||
class="group flex h-full flex-col rounded-[28px] border border-slate-200 bg-white p-6 shadow-sm transition duration-200 hover:-translate-y-0.5 hover:border-blue-200 hover:shadow-lg"
|
||||
>
|
||||
<div class="flex items-start justify-between gap-4">
|
||||
<span class="flex h-14 w-14 shrink-0 items-center justify-center rounded-2xl border border-slate-200 bg-slate-50 text-slate-900 shadow-sm">
|
||||
@if($icon)
|
||||
<x-dynamic-component :component="$icon" class="h-7 w-7" />
|
||||
<span class="flex h-16 w-16 shrink-0 items-center justify-center rounded-2xl border border-slate-200 bg-slate-50 text-slate-900 shadow-sm">
|
||||
@if($iconUrl)
|
||||
<img src="{{ $iconUrl }}" alt="{{ $category->name }}" class="h-11 w-11 object-contain">
|
||||
@else
|
||||
<span class="text-2xl font-semibold">{{ $iconLabel }}</span>
|
||||
<span class="text-3xl font-semibold">{{ $iconLabel }}</span>
|
||||
@endif
|
||||
</span>
|
||||
<span class="inline-flex items-center rounded-full border border-slate-200 bg-slate-50 px-3 py-1 text-xs font-semibold text-slate-600">
|
||||
|
||||
@ -2,6 +2,8 @@
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Modules\Category\Http\Controllers\CategoryController;
|
||||
|
||||
Route::prefix('categories')->name('categories.')->group(function () {
|
||||
Route::get('/', [CategoryController::class, 'index'])->name('index');
|
||||
Route::middleware('web')->group(function () {
|
||||
Route::prefix('categories')->name('categories.')->group(function () {
|
||||
Route::get('/', [CategoryController::class, 'index'])->name('index');
|
||||
});
|
||||
});
|
||||
|
||||
37
Modules/Conversation/App/Events/ConversationReadUpdated.php
Normal file
37
Modules/Conversation/App/Events/ConversationReadUpdated.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Conversation\App\Events;
|
||||
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class ConversationReadUpdated implements ShouldBroadcastNow
|
||||
{
|
||||
use Dispatchable;
|
||||
use InteractsWithSockets;
|
||||
use SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public int $userId,
|
||||
public array $payload,
|
||||
) {
|
||||
}
|
||||
|
||||
public function broadcastOn(): PrivateChannel
|
||||
{
|
||||
return new PrivateChannel('users.'.$this->userId.'.inbox');
|
||||
}
|
||||
|
||||
public function broadcastAs(): string
|
||||
{
|
||||
return 'inbox.read.updated';
|
||||
}
|
||||
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
return $this->payload;
|
||||
}
|
||||
}
|
||||
37
Modules/Conversation/App/Events/InboxMessageCreated.php
Normal file
37
Modules/Conversation/App/Events/InboxMessageCreated.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Conversation\App\Events;
|
||||
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
class InboxMessageCreated implements ShouldBroadcastNow
|
||||
{
|
||||
use Dispatchable;
|
||||
use InteractsWithSockets;
|
||||
use SerializesModels;
|
||||
|
||||
public function __construct(
|
||||
public int $userId,
|
||||
public array $payload,
|
||||
) {
|
||||
}
|
||||
|
||||
public function broadcastOn(): PrivateChannel
|
||||
{
|
||||
return new PrivateChannel('users.'.$this->userId.'.inbox');
|
||||
}
|
||||
|
||||
public function broadcastAs(): string
|
||||
{
|
||||
return 'inbox.message.created';
|
||||
}
|
||||
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
return $this->payload;
|
||||
}
|
||||
}
|
||||
@ -3,14 +3,16 @@
|
||||
namespace Modules\Conversation\App\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\View\View;
|
||||
use Modules\Conversation\App\Events\ConversationReadUpdated;
|
||||
use Modules\Conversation\App\Events\InboxMessageCreated;
|
||||
use Modules\Conversation\App\Models\Conversation;
|
||||
use Modules\Conversation\App\Models\ConversationMessage;
|
||||
use Modules\Conversation\App\Support\QuickMessageCatalog;
|
||||
use Modules\Listing\Models\Listing;
|
||||
use Throwable;
|
||||
|
||||
class ConversationController extends Controller
|
||||
{
|
||||
@ -24,26 +26,23 @@ class ConversationController extends Controller
|
||||
$conversations = collect();
|
||||
$selectedConversation = null;
|
||||
|
||||
if ($userId && $this->messagingTablesReady()) {
|
||||
try {
|
||||
$conversations = Conversation::inboxForUser($userId, $messageFilter);
|
||||
$selectedConversation = Conversation::resolveSelected($conversations, $request->integer('conversation'));
|
||||
if ($userId) {
|
||||
[
|
||||
'conversations' => $conversations,
|
||||
'selectedConversation' => $selectedConversation,
|
||||
'markedRead' => $markedRead,
|
||||
] = $this->resolveInboxState(
|
||||
$userId,
|
||||
$messageFilter,
|
||||
$request->integer('conversation'),
|
||||
true,
|
||||
);
|
||||
|
||||
if ($selectedConversation) {
|
||||
$selectedConversation->loadThread();
|
||||
$selectedConversation->markAsReadFor($userId);
|
||||
|
||||
$conversations = $conversations->map(function (Conversation $conversation) use ($selectedConversation): Conversation {
|
||||
if ((int) $conversation->getKey() === (int) $selectedConversation->getKey()) {
|
||||
$conversation->unread_count = 0;
|
||||
}
|
||||
|
||||
return $conversation;
|
||||
});
|
||||
}
|
||||
} catch (Throwable) {
|
||||
$conversations = collect();
|
||||
$selectedConversation = null;
|
||||
if ($selectedConversation && $markedRead) {
|
||||
broadcast(new ConversationReadUpdated(
|
||||
$userId,
|
||||
$selectedConversation->readPayloadFor($userId),
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@ -56,48 +55,77 @@ class ConversationController extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
public function start(Request $request, Listing $listing): RedirectResponse
|
||||
public function state(Request $request): JsonResponse
|
||||
{
|
||||
if (! $this->messagingTablesReady()) {
|
||||
return back()->with('error', 'Mesajlaşma altyapısı henüz hazır değil.');
|
||||
}
|
||||
$userId = (int) $request->user()->getKey();
|
||||
$messageFilter = $this->resolveMessageFilter($request);
|
||||
|
||||
[
|
||||
'conversations' => $conversations,
|
||||
'selectedConversation' => $selectedConversation,
|
||||
] = $this->resolveInboxState(
|
||||
$userId,
|
||||
$messageFilter,
|
||||
$request->integer('conversation'),
|
||||
false,
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'list_html' => $this->renderInboxList($conversations, $messageFilter, $selectedConversation),
|
||||
'thread_html' => $this->renderInboxThread($selectedConversation, $messageFilter),
|
||||
'selected_conversation_id' => $selectedConversation ? (int) $selectedConversation->getKey() : null,
|
||||
'counts' => [
|
||||
'unread_messages_total' => Conversation::unreadCountForUser($userId),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function start(Request $request, Listing $listing): RedirectResponse | JsonResponse
|
||||
{
|
||||
$user = $request->user();
|
||||
|
||||
if (! $listing->user_id) {
|
||||
return back()->with('error', 'Bu ilan için mesajlaşma açılamadı.');
|
||||
if ($request->expectsJson()) {
|
||||
return response()->json(['message' => 'A conversation cannot be started for this listing.'], 422);
|
||||
}
|
||||
|
||||
return back()->with('error', 'A conversation cannot be started for this listing.');
|
||||
}
|
||||
|
||||
if ((int) $listing->user_id === (int) $user->getKey()) {
|
||||
return back()->with('error', 'Kendi ilanına mesaj gönderemezsin.');
|
||||
if ($request->expectsJson()) {
|
||||
return response()->json(['message' => 'You cannot message your own listing.'], 422);
|
||||
}
|
||||
|
||||
return back()->with('error', 'You cannot message your own listing.');
|
||||
}
|
||||
|
||||
$conversation = Conversation::openForListingBuyer($listing, (int) $user->getKey());
|
||||
|
||||
$user->favoriteListings()->syncWithoutDetaching([$listing->getKey()]);
|
||||
|
||||
$messageBody = trim((string) $request->string('message'));
|
||||
|
||||
if ($messageBody !== '') {
|
||||
$message = $conversation->messages()->create([
|
||||
'sender_id' => $user->getKey(),
|
||||
'body' => $messageBody,
|
||||
]);
|
||||
if ($request->expectsJson() && $messageBody === '') {
|
||||
return response()->json(['message' => 'Message cannot be empty.'], 422);
|
||||
}
|
||||
|
||||
$conversation->forceFill(['last_message_at' => $message->created_at])->save();
|
||||
$conversation = Conversation::openForListingBuyer($listing, (int) $user->getKey());
|
||||
$user->rememberListing($listing);
|
||||
|
||||
$message = null;
|
||||
if ($messageBody !== '') {
|
||||
$message = $conversation->createMessageFor((int) $user->getKey(), $messageBody);
|
||||
$this->broadcastMessageCreated($conversation, $message, (int) $user->getKey());
|
||||
}
|
||||
|
||||
if ($request->expectsJson()) {
|
||||
return $this->conversationJsonResponse($conversation, $message, (int) $user->getKey());
|
||||
}
|
||||
|
||||
return redirect()
|
||||
->route('panel.inbox.index', array_merge($this->inboxFilters($request), ['conversation' => $conversation->getKey()]))
|
||||
->with('success', $messageBody !== '' ? 'Mesaj gönderildi.' : 'Sohbet açıldı.');
|
||||
->with('success', $messageBody !== '' ? 'Message sent.' : 'Conversation started.');
|
||||
}
|
||||
|
||||
public function send(Request $request, Conversation $conversation): RedirectResponse
|
||||
public function send(Request $request, Conversation $conversation): RedirectResponse | JsonResponse
|
||||
{
|
||||
if (! $this->messagingTablesReady()) {
|
||||
return back()->with('error', 'Mesajlaşma altyapısı henüz hazır değil.');
|
||||
}
|
||||
|
||||
$user = $request->user();
|
||||
$userId = (int) $user->getKey();
|
||||
|
||||
@ -112,19 +140,60 @@ class ConversationController extends Controller
|
||||
$messageBody = trim($payload['message']);
|
||||
|
||||
if ($messageBody === '') {
|
||||
return back()->with('error', 'Mesaj boş olamaz.');
|
||||
if ($request->expectsJson()) {
|
||||
return response()->json(['message' => 'Message cannot be empty.'], 422);
|
||||
}
|
||||
|
||||
return back()->with('error', 'Message cannot be empty.');
|
||||
}
|
||||
|
||||
$message = $conversation->messages()->create([
|
||||
'sender_id' => $userId,
|
||||
'body' => $messageBody,
|
||||
]);
|
||||
$message = $conversation->createMessageFor($userId, $messageBody);
|
||||
$this->broadcastMessageCreated($conversation, $message, $userId);
|
||||
|
||||
$conversation->forceFill(['last_message_at' => $message->created_at])->save();
|
||||
if ($request->expectsJson()) {
|
||||
return $this->conversationJsonResponse($conversation, $message, $userId);
|
||||
}
|
||||
|
||||
return redirect()
|
||||
->route('panel.inbox.index', array_merge($this->inboxFilters($request), ['conversation' => $conversation->getKey()]))
|
||||
->with('success', 'Mesaj gönderildi.');
|
||||
->with('success', 'Message sent.');
|
||||
}
|
||||
|
||||
public function read(Request $request, Conversation $conversation): JsonResponse
|
||||
{
|
||||
$userId = (int) $request->user()->getKey();
|
||||
abort_unless($conversation->hasParticipant($userId), 403);
|
||||
|
||||
$updated = $conversation->markAsReadFor($userId);
|
||||
$payload = $conversation->readPayloadFor($userId);
|
||||
|
||||
if ($updated > 0) {
|
||||
broadcast(new ConversationReadUpdated($userId, $payload))->toOthers();
|
||||
}
|
||||
|
||||
return response()->json($payload);
|
||||
}
|
||||
|
||||
private function conversationJsonResponse(Conversation $conversation, ?ConversationMessage $message, int $userId): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'conversation_id' => (int) $conversation->getKey(),
|
||||
'send_url' => route('conversations.messages.send', $conversation),
|
||||
'read_url' => route('conversations.read', $conversation),
|
||||
'conversation' => [
|
||||
'id' => (int) $conversation->getKey(),
|
||||
'unread_count' => $conversation->unreadCountForParticipant($userId),
|
||||
],
|
||||
'counts' => [
|
||||
'unread_messages_total' => Conversation::unreadCountForUser($userId),
|
||||
],
|
||||
'message' => $message ? $this->messagePayload($message, $userId) : null,
|
||||
]);
|
||||
}
|
||||
|
||||
private function messagePayload(ConversationMessage $message, int $userId): array
|
||||
{
|
||||
return $message->toRealtimePayloadFor($userId);
|
||||
}
|
||||
|
||||
private function inboxFilters(Request $request): array
|
||||
@ -141,12 +210,76 @@ class ConversationController extends Controller
|
||||
return in_array($messageFilter, ['all', 'unread', 'important'], true) ? $messageFilter : 'all';
|
||||
}
|
||||
|
||||
private function messagingTablesReady(): bool
|
||||
private function resolveInboxState(
|
||||
int $userId,
|
||||
string $messageFilter,
|
||||
?int $conversationId,
|
||||
bool $markSelectedRead,
|
||||
): array {
|
||||
$conversations = Conversation::inboxForUser($userId, $messageFilter);
|
||||
$selectedConversation = Conversation::resolveSelected($conversations, $conversationId);
|
||||
$markedRead = false;
|
||||
|
||||
if ($selectedConversation) {
|
||||
$selectedConversation->loadThread();
|
||||
|
||||
if ($markSelectedRead) {
|
||||
$markedRead = $selectedConversation->markAsReadFor($userId) > 0;
|
||||
$selectedConversation->unread_count = 0;
|
||||
|
||||
$conversations = $conversations->map(function (Conversation $conversation) use ($selectedConversation): Conversation {
|
||||
if ((int) $conversation->getKey() === (int) $selectedConversation->getKey()) {
|
||||
$conversation->unread_count = 0;
|
||||
}
|
||||
|
||||
return $conversation;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'conversations' => $conversations,
|
||||
'selectedConversation' => $selectedConversation,
|
||||
'markedRead' => $markedRead,
|
||||
];
|
||||
}
|
||||
|
||||
private function renderInboxList($conversations, string $messageFilter, ?Conversation $selectedConversation): string
|
||||
{
|
||||
try {
|
||||
return Schema::hasTable('conversations') && Schema::hasTable('conversation_messages');
|
||||
} catch (Throwable) {
|
||||
return false;
|
||||
return view('conversation::partials.inbox-list-pane', [
|
||||
'conversations' => $conversations,
|
||||
'messageFilter' => $messageFilter,
|
||||
'selectedConversation' => $selectedConversation,
|
||||
])->render();
|
||||
}
|
||||
|
||||
private function renderInboxThread(?Conversation $selectedConversation, string $messageFilter): string
|
||||
{
|
||||
return view('conversation::partials.inbox-thread-pane', [
|
||||
'selectedConversation' => $selectedConversation,
|
||||
'messageFilter' => $messageFilter,
|
||||
'quickMessages' => QuickMessageCatalog::all(),
|
||||
])->render();
|
||||
}
|
||||
|
||||
private function broadcastMessageCreated(
|
||||
Conversation $conversation,
|
||||
ConversationMessage $message,
|
||||
int $senderId,
|
||||
): void {
|
||||
foreach ($conversation->participantIds() as $participantId) {
|
||||
$event = new InboxMessageCreated(
|
||||
$participantId,
|
||||
$conversation->realtimePayloadFor($participantId, $message),
|
||||
);
|
||||
|
||||
if ($participantId === $senderId) {
|
||||
broadcast($event)->toOthers();
|
||||
continue;
|
||||
}
|
||||
|
||||
broadcast($event);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -121,13 +121,59 @@ class Conversation extends Model
|
||||
{
|
||||
$this->load([
|
||||
'listing:id,title,price,currency,user_id',
|
||||
'messages' => fn (Builder $query) => $query->with('sender:id,name')->orderBy('created_at'),
|
||||
'messages' => fn ($query) => $query->with('sender:id,name')->ordered(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function markAsReadFor(int $userId): void
|
||||
public function hasParticipant(int $userId): bool
|
||||
{
|
||||
ConversationMessage::query()
|
||||
return (int) $this->buyer_id === $userId || (int) $this->seller_id === $userId;
|
||||
}
|
||||
|
||||
public function participantIds(): array
|
||||
{
|
||||
return collect([$this->buyer_id, $this->seller_id])
|
||||
->filter()
|
||||
->map(fn ($id): int => (int) $id)
|
||||
->unique()
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
public function partnerFor(int $userId): ?User
|
||||
{
|
||||
$this->loadMissing([
|
||||
'buyer:id,name,email',
|
||||
'seller:id,name,email',
|
||||
]);
|
||||
|
||||
return (int) $this->buyer_id === $userId ? $this->seller : $this->buyer;
|
||||
}
|
||||
|
||||
public function createMessageFor(int $senderId, string $body): ConversationMessage
|
||||
{
|
||||
$message = $this->messages()->create([
|
||||
'sender_id' => $senderId,
|
||||
'body' => $body,
|
||||
]);
|
||||
|
||||
$this->forceFill(['last_message_at' => $message->created_at])->save();
|
||||
|
||||
return $message->loadMissing('sender:id,name');
|
||||
}
|
||||
|
||||
public function unreadCountForParticipant(int $userId): int
|
||||
{
|
||||
return (int) ConversationMessage::query()
|
||||
->where('conversation_id', $this->getKey())
|
||||
->where('sender_id', '!=', $userId)
|
||||
->whereNull('read_at')
|
||||
->count();
|
||||
}
|
||||
|
||||
public function markAsReadFor(int $userId): int
|
||||
{
|
||||
return ConversationMessage::query()
|
||||
->where('conversation_id', $this->getKey())
|
||||
->where('sender_id', '!=', $userId)
|
||||
->whereNull('read_at')
|
||||
@ -137,6 +183,78 @@ class Conversation extends Model
|
||||
]);
|
||||
}
|
||||
|
||||
public function listingImageUrl(): ?string
|
||||
{
|
||||
$this->loadMissing('listing');
|
||||
|
||||
$url = $this->listing?->primaryImageUrl('thumb', 'desktop');
|
||||
|
||||
return is_string($url) && trim($url) !== '' ? $url : null;
|
||||
}
|
||||
|
||||
public function summaryPayloadFor(int $viewerId): array
|
||||
{
|
||||
$this->loadMissing([
|
||||
'listing:id,title,price,currency,user_id',
|
||||
'buyer:id,name,email',
|
||||
'seller:id,name,email',
|
||||
'lastMessage',
|
||||
'lastMessage.sender:id,name',
|
||||
]);
|
||||
|
||||
$lastMessage = $this->lastMessage;
|
||||
$partner = $this->partnerFor($viewerId);
|
||||
|
||||
return [
|
||||
'id' => (int) $this->getKey(),
|
||||
'unread_count' => $this->unread_count ?? $this->unreadCountForParticipant($viewerId),
|
||||
'listing' => [
|
||||
'id' => (int) ($this->listing?->getKey() ?? 0),
|
||||
'title' => (string) ($this->listing?->title ?? 'Listing removed'),
|
||||
'image_url' => $this->listingImageUrl(),
|
||||
],
|
||||
'partner' => [
|
||||
'id' => (int) ($partner?->getKey() ?? 0),
|
||||
'name' => (string) ($partner?->name ?? 'User'),
|
||||
],
|
||||
'last_message' => $lastMessage
|
||||
? $lastMessage->toRealtimePayloadFor($viewerId)
|
||||
: null,
|
||||
'last_message_at' => $this->last_message_at?->toIso8601String(),
|
||||
];
|
||||
}
|
||||
|
||||
public function realtimePayloadFor(int $viewerId, ConversationMessage $message): array
|
||||
{
|
||||
$summary = $this->summaryPayloadFor($viewerId);
|
||||
|
||||
return [
|
||||
'conversation' => [
|
||||
'id' => (int) $this->getKey(),
|
||||
'unread_count' => $this->unreadCountForParticipant($viewerId),
|
||||
],
|
||||
'listing' => $summary['listing'],
|
||||
'partner' => $summary['partner'],
|
||||
'message' => $message->toRealtimePayloadFor($viewerId),
|
||||
'counts' => [
|
||||
'unread_messages_total' => static::unreadCountForUser($viewerId),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function readPayloadFor(int $viewerId): array
|
||||
{
|
||||
return [
|
||||
'conversation' => [
|
||||
'id' => (int) $this->getKey(),
|
||||
'unread_count' => $this->unreadCountForParticipant($viewerId),
|
||||
],
|
||||
'counts' => [
|
||||
'unread_messages_total' => static::unreadCountForUser($viewerId),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public static function openForListingBuyer(Listing $listing, int $buyerId): self
|
||||
{
|
||||
$conversation = static::query()->firstOrCreate(
|
||||
@ -165,4 +283,51 @@ class Conversation extends Model
|
||||
|
||||
return is_null($value) ? null : (int) $value;
|
||||
}
|
||||
|
||||
public static function detailForBuyerListing(int $listingId, int $buyerId): ?self
|
||||
{
|
||||
$conversationId = static::buyerListingConversationId($listingId, $buyerId);
|
||||
|
||||
if (! $conversationId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$conversation = static::query()
|
||||
->forUser($buyerId)
|
||||
->find($conversationId);
|
||||
|
||||
if (! $conversation) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$conversation->loadThread();
|
||||
$conversation->loadCount([
|
||||
'messages as unread_count' => fn (Builder $query) => $query
|
||||
->where('sender_id', '!=', $buyerId)
|
||||
->whereNull('read_at'),
|
||||
]);
|
||||
|
||||
return $conversation;
|
||||
}
|
||||
|
||||
public static function listingMapForBuyer(int $buyerId, array $listingIds = []): array
|
||||
{
|
||||
return static::query()
|
||||
->where('buyer_id', $buyerId)
|
||||
->when($listingIds !== [], fn (Builder $query): Builder => $query->whereIn('listing_id', $listingIds))
|
||||
->pluck('id', 'listing_id')
|
||||
->map(fn ($conversationId): int => (int) $conversationId)
|
||||
->all();
|
||||
}
|
||||
|
||||
public static function unreadCountForUser(int $userId): int
|
||||
{
|
||||
return (int) ConversationMessage::query()
|
||||
->whereHas('conversation', function (Builder $query) use ($userId): void {
|
||||
$query->forUser($userId);
|
||||
})
|
||||
->where('sender_id', '!=', $userId)
|
||||
->whereNull('read_at')
|
||||
->count();
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@ namespace Modules\Conversation\App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Modules\User\App\Models\User;
|
||||
|
||||
class ConversationMessage extends Model
|
||||
@ -23,4 +24,23 @@ class ConversationMessage extends Model
|
||||
{
|
||||
return $this->belongsTo(User::class, 'sender_id');
|
||||
}
|
||||
|
||||
public function scopeOrdered(Builder $query): Builder
|
||||
{
|
||||
return $query->orderBy('created_at');
|
||||
}
|
||||
|
||||
public function toRealtimePayloadFor(int $viewerId): array
|
||||
{
|
||||
$this->loadMissing('sender:id,name');
|
||||
|
||||
return [
|
||||
'id' => (int) $this->getKey(),
|
||||
'body' => (string) $this->body,
|
||||
'time' => $this->created_at?->format('H:i') ?? now()->format('H:i'),
|
||||
'sender_id' => (int) $this->sender_id,
|
||||
'sender_name' => (string) ($this->sender?->name ?? 'User'),
|
||||
'is_mine' => (int) $this->sender_id === $viewerId,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,18 +2,21 @@
|
||||
|
||||
namespace Modules\Conversation\App\Providers;
|
||||
|
||||
use Illuminate\Support\Facades\Broadcast;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class ConversationServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function boot(): void
|
||||
{
|
||||
$this->loadMigrationsFrom(module_path('Conversation', 'database/migrations'));
|
||||
$this->loadMigrationsFrom(module_path('Conversation', 'Database/migrations'));
|
||||
$this->loadRoutesFrom(module_path('Conversation', 'routes/web.php'));
|
||||
$this->loadViewsFrom(module_path('Conversation', 'resources/views'), 'conversation');
|
||||
|
||||
Broadcast::channel('users.{id}.inbox', function ($user, $id): bool {
|
||||
return (int) $user->getKey() === (int) $id;
|
||||
});
|
||||
}
|
||||
|
||||
public function register(): void
|
||||
{
|
||||
}
|
||||
public function register(): void {}
|
||||
}
|
||||
|
||||
@ -7,10 +7,10 @@ class QuickMessageCatalog
|
||||
public static function all(): array
|
||||
{
|
||||
return [
|
||||
'Merhaba',
|
||||
'İlan hâlâ satışta mı?',
|
||||
'Son fiyat nedir?',
|
||||
'Teşekkürler',
|
||||
'Hi',
|
||||
'Is this listing still available?',
|
||||
'What is your best price?',
|
||||
'Thanks',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
156
Modules/Conversation/Database/Seeders/ConversationDemoSeeder.php
Normal file
156
Modules/Conversation/Database/Seeders/ConversationDemoSeeder.php
Normal file
@ -0,0 +1,156 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Conversation\Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use Modules\Conversation\App\Models\Conversation;
|
||||
use Modules\Conversation\App\Models\ConversationMessage;
|
||||
use Modules\Listing\Models\Listing;
|
||||
use Modules\User\App\Models\User;
|
||||
use Modules\User\App\Support\DemoUserCatalog;
|
||||
|
||||
class ConversationDemoSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$users = User::query()
|
||||
->whereIn('email', DemoUserCatalog::emails())
|
||||
->orderBy('email')
|
||||
->get()
|
||||
->values();
|
||||
|
||||
if ($users->count() < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
ConversationMessage::query()
|
||||
->whereHas('conversation', fn ($query) => $query->whereIn('buyer_id', $users->pluck('id'))->orWhereIn('seller_id', $users->pluck('id')))
|
||||
->delete();
|
||||
|
||||
Conversation::query()
|
||||
->whereIn('buyer_id', $users->pluck('id'))
|
||||
->orWhereIn('seller_id', $users->pluck('id'))
|
||||
->delete();
|
||||
|
||||
foreach ($users as $index => $buyer) {
|
||||
$primarySeller = $users->get(($index + 1) % $users->count());
|
||||
$secondarySeller = $users->get(($index + 2) % $users->count());
|
||||
|
||||
if (! $primarySeller instanceof User || ! $secondarySeller instanceof User) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$primaryListing = Listing::query()
|
||||
->where('user_id', $primarySeller->getKey())
|
||||
->where('status', 'active')
|
||||
->orderBy('id')
|
||||
->first();
|
||||
|
||||
$secondaryListing = Listing::query()
|
||||
->where('user_id', $secondarySeller->getKey())
|
||||
->where('status', 'active')
|
||||
->orderBy('id')
|
||||
->first();
|
||||
|
||||
$this->seedConversationThread(
|
||||
$primarySeller,
|
||||
$buyer,
|
||||
$primaryListing,
|
||||
$this->messagePayloads($index, false)
|
||||
);
|
||||
|
||||
$this->seedConversationThread(
|
||||
$secondarySeller,
|
||||
$buyer,
|
||||
$secondaryListing,
|
||||
$this->messagePayloads($index, true)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private function seedConversationThread(
|
||||
User $seller,
|
||||
User $buyer,
|
||||
?Listing $listing,
|
||||
array $messages
|
||||
): void {
|
||||
if (! $listing || (int) $seller->getKey() === (int) $buyer->getKey()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$conversation = Conversation::updateOrCreate(
|
||||
[
|
||||
'listing_id' => $listing->getKey(),
|
||||
'buyer_id' => $buyer->getKey(),
|
||||
],
|
||||
[
|
||||
'seller_id' => $seller->getKey(),
|
||||
'last_message_at' => now(),
|
||||
]
|
||||
);
|
||||
|
||||
$lastMessageAt = null;
|
||||
|
||||
foreach ($messages as $payload) {
|
||||
$createdAt = now()->subHours((int) $payload['hours_ago']);
|
||||
$sender = ($payload['sender'] ?? 'buyer') === 'seller' ? $seller : $buyer;
|
||||
$readAfterMinutes = $payload['read_after_minutes'];
|
||||
$readAt = is_numeric($readAfterMinutes) ? $createdAt->copy()->addMinutes((int) $readAfterMinutes) : null;
|
||||
|
||||
$message = new ConversationMessage;
|
||||
$message->forceFill([
|
||||
'conversation_id' => $conversation->getKey(),
|
||||
'sender_id' => $sender->getKey(),
|
||||
'body' => (string) $payload['body'],
|
||||
'read_at' => $readAt,
|
||||
'created_at' => $createdAt,
|
||||
'updated_at' => $readAt ?? $createdAt,
|
||||
])->save();
|
||||
|
||||
$lastMessageAt = $createdAt;
|
||||
}
|
||||
|
||||
if ($lastMessageAt) {
|
||||
$conversation->forceFill([
|
||||
'seller_id' => $seller->getKey(),
|
||||
'last_message_at' => $lastMessageAt,
|
||||
'updated_at' => $lastMessageAt,
|
||||
])->saveQuietly();
|
||||
}
|
||||
}
|
||||
|
||||
private function messagePayloads(int $index, bool $secondary): array
|
||||
{
|
||||
$openingMessages = [
|
||||
'Is this listing still available?',
|
||||
'Can you share the best price?',
|
||||
'Would pickup this evening work for you?',
|
||||
'Can you confirm the condition details?',
|
||||
'Do you have any more photos?',
|
||||
];
|
||||
|
||||
$sellerReplies = [
|
||||
'Yes, it is available.',
|
||||
'I can offer a small discount.',
|
||||
'This evening works for me.',
|
||||
'Everything is in clean condition.',
|
||||
'I can send more photos in a minute.',
|
||||
];
|
||||
|
||||
$closingMessages = [
|
||||
'Great, I will message again before I leave.',
|
||||
'Perfect. I can arrange pickup.',
|
||||
'Thanks. That sounds good to me.',
|
||||
'Understood. I am interested.',
|
||||
'Nice. I will keep this saved.',
|
||||
];
|
||||
|
||||
$offset = ($index + ($secondary ? 2 : 0)) % count($openingMessages);
|
||||
|
||||
return [
|
||||
['sender' => 'buyer', 'body' => $openingMessages[$offset], 'hours_ago' => 30 - $index, 'read_after_minutes' => 5],
|
||||
['sender' => 'seller', 'body' => $sellerReplies[$offset], 'hours_ago' => 28 - $index, 'read_after_minutes' => 8],
|
||||
['sender' => 'buyer', 'body' => $closingMessages[$offset], 'hours_ago' => 4 + $index, 'read_after_minutes' => $secondary ? 6 : null],
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('conversations', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('listing_id')->constrained('listings')->cascadeOnDelete();
|
||||
$table->foreignId('seller_id')->constrained('users')->cascadeOnDelete();
|
||||
$table->foreignId('buyer_id')->constrained('users')->cascadeOnDelete();
|
||||
$table->timestamp('last_message_at')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['listing_id', 'buyer_id']);
|
||||
$table->index(['seller_id', 'last_message_at']);
|
||||
$table->index(['buyer_id', 'last_message_at']);
|
||||
});
|
||||
|
||||
Schema::create('conversation_messages', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('conversation_id')->constrained('conversations')->cascadeOnDelete();
|
||||
$table->foreignId('sender_id')->constrained('users')->cascadeOnDelete();
|
||||
$table->text('body');
|
||||
$table->timestamp('read_at')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->index(['conversation_id', 'created_at']);
|
||||
$table->index(['conversation_id', 'read_at']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('conversation_messages');
|
||||
Schema::dropIfExists('conversations');
|
||||
}
|
||||
};
|
||||
@ -1,46 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
if (! Schema::hasTable('conversations')) {
|
||||
Schema::create('conversations', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('listing_id')->constrained('listings')->cascadeOnDelete();
|
||||
$table->foreignId('seller_id')->constrained('users')->cascadeOnDelete();
|
||||
$table->foreignId('buyer_id')->constrained('users')->cascadeOnDelete();
|
||||
$table->timestamp('last_message_at')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['listing_id', 'buyer_id']);
|
||||
$table->index(['seller_id', 'last_message_at']);
|
||||
$table->index(['buyer_id', 'last_message_at']);
|
||||
});
|
||||
}
|
||||
|
||||
if (! Schema::hasTable('conversation_messages')) {
|
||||
Schema::create('conversation_messages', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('conversation_id')->constrained('conversations')->cascadeOnDelete();
|
||||
$table->foreignId('sender_id')->constrained('users')->cascadeOnDelete();
|
||||
$table->text('body');
|
||||
$table->timestamp('read_at')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->index(['conversation_id', 'created_at']);
|
||||
$table->index(['conversation_id', 'read_at']);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('conversation_messages');
|
||||
Schema::dropIfExists('conversations');
|
||||
}
|
||||
};
|
||||
@ -1,118 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Conversation\Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Modules\Conversation\App\Models\Conversation;
|
||||
use Modules\Conversation\App\Models\ConversationMessage;
|
||||
use Modules\Listing\Models\Listing;
|
||||
use Modules\User\App\Models\User;
|
||||
|
||||
class ConversationDemoSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
if (! $this->conversationTablesExist()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$admin = User::query()->where('email', 'a@a.com')->first();
|
||||
$partner = User::query()->where('email', 'b@b.com')->first();
|
||||
|
||||
if (! $admin || ! $partner) {
|
||||
return;
|
||||
}
|
||||
|
||||
$listings = Listing::query()
|
||||
->where('user_id', $admin->getKey())
|
||||
->where('status', 'active')
|
||||
->orderBy('id')
|
||||
->take(2)
|
||||
->get();
|
||||
|
||||
if ($listings->count() < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->seedConversationThread(
|
||||
$listings->get(0),
|
||||
$admin,
|
||||
$partner,
|
||||
[
|
||||
['sender' => 'partner', 'body' => 'Hi, is this still available?', 'hours_ago' => 30, 'read_after_minutes' => 4],
|
||||
['sender' => 'admin', 'body' => 'Yes, it is available. I can share more photos.', 'hours_ago' => 29, 'read_after_minutes' => 7],
|
||||
['sender' => 'partner', 'body' => 'Perfect. Can we meet tomorrow afternoon?', 'hours_ago' => 4, 'read_after_minutes' => null],
|
||||
]
|
||||
);
|
||||
|
||||
$this->seedConversationThread(
|
||||
$listings->get(1),
|
||||
$admin,
|
||||
$partner,
|
||||
[
|
||||
['sender' => 'partner', 'body' => 'Can you confirm the final price?', 'hours_ago' => 20, 'read_after_minutes' => 8],
|
||||
['sender' => 'admin', 'body' => 'I can do a small discount if you pick it up today.', 'hours_ago' => 18, 'read_after_minutes' => null],
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
private function conversationTablesExist(): bool
|
||||
{
|
||||
return Schema::hasTable('conversations') && Schema::hasTable('conversation_messages');
|
||||
}
|
||||
|
||||
private function seedConversationThread(
|
||||
?Listing $listing,
|
||||
User $admin,
|
||||
User $partner,
|
||||
array $messages
|
||||
): void {
|
||||
if (! $listing) {
|
||||
return;
|
||||
}
|
||||
|
||||
$conversation = Conversation::updateOrCreate(
|
||||
[
|
||||
'listing_id' => $listing->getKey(),
|
||||
'buyer_id' => $partner->getKey(),
|
||||
],
|
||||
[
|
||||
'seller_id' => $admin->getKey(),
|
||||
'last_message_at' => now(),
|
||||
]
|
||||
);
|
||||
|
||||
ConversationMessage::query()
|
||||
->where('conversation_id', $conversation->getKey())
|
||||
->delete();
|
||||
|
||||
$lastMessageAt = null;
|
||||
|
||||
foreach ($messages as $payload) {
|
||||
$createdAt = now()->subHours((int) $payload['hours_ago']);
|
||||
$sender = ($payload['sender'] ?? 'partner') === 'admin' ? $admin : $partner;
|
||||
$readAfterMinutes = $payload['read_after_minutes'];
|
||||
$readAt = is_numeric($readAfterMinutes) ? $createdAt->copy()->addMinutes((int) $readAfterMinutes) : null;
|
||||
|
||||
$message = new ConversationMessage();
|
||||
$message->forceFill([
|
||||
'conversation_id' => $conversation->getKey(),
|
||||
'sender_id' => $sender->getKey(),
|
||||
'body' => (string) $payload['body'],
|
||||
'read_at' => $readAt,
|
||||
'created_at' => $createdAt,
|
||||
'updated_at' => $readAt ?? $createdAt,
|
||||
])->save();
|
||||
|
||||
$lastMessageAt = $createdAt;
|
||||
}
|
||||
|
||||
$conversation->forceFill([
|
||||
'seller_id' => $admin->getKey(),
|
||||
'last_message_at' => $lastMessageAt,
|
||||
'updated_at' => $lastMessageAt,
|
||||
])->saveQuietly();
|
||||
}
|
||||
}
|
||||
605
Modules/Conversation/resources/assets/js/conversation.js
Normal file
605
Modules/Conversation/resources/assets/js/conversation.js
Normal file
@ -0,0 +1,605 @@
|
||||
const onReady = (callback) => {
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', callback, { once: true });
|
||||
return;
|
||||
}
|
||||
|
||||
callback();
|
||||
};
|
||||
|
||||
const csrfToken = () => document.querySelector('meta[name="csrf-token"]')?.getAttribute('content') || '';
|
||||
|
||||
const socketHeaders = () => {
|
||||
const socketId = window.Echo?.socketId?.();
|
||||
|
||||
return socketId ? { 'X-Socket-ID': socketId } : {};
|
||||
};
|
||||
|
||||
const jsonHeaders = () => ({
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
||||
'X-CSRF-TOKEN': csrfToken(),
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
...socketHeaders(),
|
||||
});
|
||||
|
||||
const toFormBody = (values) => {
|
||||
const params = new URLSearchParams();
|
||||
|
||||
Object.entries(values).forEach(([key, value]) => {
|
||||
if (value === null || value === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
params.append(key, String(value));
|
||||
});
|
||||
|
||||
return params.toString();
|
||||
};
|
||||
|
||||
const formatCount = (count) => (count > 99 ? '99+' : String(count));
|
||||
|
||||
const setBadgeCount = (badge, count) => {
|
||||
if (!badge) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!count || count < 1) {
|
||||
badge.textContent = '0';
|
||||
badge.classList.add('hidden');
|
||||
return;
|
||||
}
|
||||
|
||||
badge.textContent = formatCount(count);
|
||||
badge.classList.remove('hidden');
|
||||
};
|
||||
|
||||
const updateHeaderInboxBadge = (count) => {
|
||||
setBadgeCount(document.querySelector('[data-header-inbox-badge]'), count);
|
||||
};
|
||||
|
||||
const subscribeInboxChannel = () => {
|
||||
const body = document.body;
|
||||
const userId = String(body.dataset.authUserId || '').trim();
|
||||
const channelName = String(body.dataset.inboxChannel || '').trim();
|
||||
|
||||
if (!userId || !channelName || !window.Echo?.private || window.__ocInboxChannel === channelName) {
|
||||
return;
|
||||
}
|
||||
|
||||
const channel = window.Echo.private(channelName);
|
||||
|
||||
channel.listen('.inbox.message.created', (payload) => {
|
||||
updateHeaderInboxBadge(payload?.counts?.unread_messages_total ?? 0);
|
||||
document.dispatchEvent(new CustomEvent('oc:inbox-message-created', { detail: payload }));
|
||||
});
|
||||
|
||||
channel.listen('.inbox.read.updated', (payload) => {
|
||||
updateHeaderInboxBadge(payload?.counts?.unread_messages_total ?? 0);
|
||||
document.dispatchEvent(new CustomEvent('oc:inbox-read-updated', { detail: payload }));
|
||||
});
|
||||
|
||||
window.__ocInboxChannel = channelName;
|
||||
};
|
||||
|
||||
const buildMessageItem = (message) => {
|
||||
const item = document.createElement('div');
|
||||
item.dataset.messageId = String(message.id);
|
||||
item.className = `lt-chat-item${message.is_mine ? ' is-mine' : ''}`;
|
||||
|
||||
const bubble = document.createElement('div');
|
||||
bubble.className = 'lt-chat-bubble';
|
||||
bubble.textContent = message.body;
|
||||
|
||||
const time = document.createElement('span');
|
||||
time.className = 'lt-chat-time';
|
||||
time.textContent = message.time || '';
|
||||
|
||||
item.append(bubble, time);
|
||||
|
||||
return item;
|
||||
};
|
||||
|
||||
const appendInlineChatMessage = (thread, emptyState, message) => {
|
||||
if (!thread || !message?.body || thread.querySelector(`[data-message-id="${message.id}"]`)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const item = buildMessageItem(message);
|
||||
thread.appendChild(item);
|
||||
emptyState?.classList.add('is-hidden');
|
||||
thread.scrollTop = thread.scrollHeight;
|
||||
};
|
||||
|
||||
const initInlineListingChat = () => {
|
||||
const root = document.querySelector('[data-inline-chat]');
|
||||
|
||||
if (!root || root.dataset.chatBound === '1') {
|
||||
return;
|
||||
}
|
||||
|
||||
root.dataset.chatBound = '1';
|
||||
|
||||
const launcher = root.querySelector('[data-inline-chat-launcher]');
|
||||
const panel = root.querySelector('[data-inline-chat-panel]');
|
||||
const thread = root.querySelector('[data-inline-chat-thread]');
|
||||
const emptyState = root.querySelector('[data-inline-chat-empty]');
|
||||
const form = root.querySelector('[data-inline-chat-form]');
|
||||
const input = root.querySelector('[data-inline-chat-input]');
|
||||
const error = root.querySelector('[data-inline-chat-error]');
|
||||
const submitButton = root.querySelector('[data-inline-chat-submit]');
|
||||
const launcherBadge = root.querySelector('[data-inline-chat-badge]');
|
||||
|
||||
const currentConversationId = () => String(root.dataset.conversationId || '').trim();
|
||||
|
||||
const resolveReadUrl = () => {
|
||||
if (root.dataset.readUrl) {
|
||||
return root.dataset.readUrl;
|
||||
}
|
||||
|
||||
const conversationId = currentConversationId();
|
||||
const template = String(root.dataset.readUrlTemplate || '');
|
||||
|
||||
return conversationId && template ? template.replace('__CONVERSATION__', conversationId) : '';
|
||||
};
|
||||
|
||||
const setState = (state) => {
|
||||
const isOpen = state === 'open' || state === 'sending';
|
||||
|
||||
root.dataset.state = state;
|
||||
root.classList.toggle('is-open', isOpen);
|
||||
root.classList.toggle('is-collapsed', state === 'collapsed');
|
||||
root.classList.toggle('is-sending', state === 'sending');
|
||||
|
||||
launcher?.toggleAttribute('hidden', isOpen);
|
||||
panel?.toggleAttribute('hidden', !isOpen);
|
||||
|
||||
if (isOpen) {
|
||||
window.requestAnimationFrame(() => {
|
||||
thread?.scrollTo({ top: thread.scrollHeight });
|
||||
input?.focus();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const showError = (message) => {
|
||||
if (!error) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!message) {
|
||||
error.textContent = '';
|
||||
error.classList.add('is-hidden');
|
||||
return;
|
||||
}
|
||||
|
||||
error.textContent = message;
|
||||
error.classList.remove('is-hidden');
|
||||
};
|
||||
|
||||
const setUnreadCount = (count) => {
|
||||
setBadgeCount(launcherBadge, count);
|
||||
};
|
||||
|
||||
const markConversationRead = async () => {
|
||||
const readUrl = resolveReadUrl();
|
||||
|
||||
if (!readUrl || !currentConversationId()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await fetch(readUrl, {
|
||||
method: 'POST',
|
||||
headers: jsonHeaders(),
|
||||
});
|
||||
|
||||
const payload = await response.json().catch(() => ({}));
|
||||
|
||||
if (!response.ok) {
|
||||
return;
|
||||
}
|
||||
|
||||
setUnreadCount(payload?.conversation?.unread_count ?? 0);
|
||||
updateHeaderInboxBadge(payload?.counts?.unread_messages_total ?? 0);
|
||||
};
|
||||
|
||||
const openChat = async (event) => {
|
||||
event?.preventDefault();
|
||||
event?.stopPropagation();
|
||||
|
||||
if (root.dataset.state === 'open' || root.dataset.state === 'sending') {
|
||||
return;
|
||||
}
|
||||
|
||||
showError('');
|
||||
setState('open');
|
||||
await markConversationRead();
|
||||
};
|
||||
|
||||
document.querySelectorAll('[data-inline-chat-trigger]').forEach((button) => {
|
||||
button.addEventListener('click', openChat);
|
||||
});
|
||||
|
||||
launcher?.addEventListener('click', openChat);
|
||||
|
||||
root.querySelector('[data-inline-chat-close]')?.addEventListener('click', (event) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
if (root.dataset.state === 'collapsed') {
|
||||
return;
|
||||
}
|
||||
|
||||
showError('');
|
||||
setState('collapsed');
|
||||
});
|
||||
|
||||
root.addEventListener('click', (event) => {
|
||||
if (event.target.closest('[data-inline-chat-panel]')) {
|
||||
event.stopPropagation();
|
||||
}
|
||||
});
|
||||
|
||||
form?.addEventListener('submit', async (event) => {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
|
||||
if (!input || !submitButton || root.dataset.state === 'sending') {
|
||||
return;
|
||||
}
|
||||
|
||||
const message = input.value.trim();
|
||||
if (message === '') {
|
||||
showError('Message cannot be empty.');
|
||||
input.focus();
|
||||
return;
|
||||
}
|
||||
|
||||
const targetUrl = root.dataset.sendUrl || root.dataset.startUrl;
|
||||
if (!targetUrl) {
|
||||
showError('Messaging is not available right now.');
|
||||
return;
|
||||
}
|
||||
|
||||
showError('');
|
||||
submitButton.disabled = true;
|
||||
setState('sending');
|
||||
|
||||
try {
|
||||
const response = await fetch(targetUrl, {
|
||||
method: 'POST',
|
||||
headers: jsonHeaders(),
|
||||
body: toFormBody({ message }),
|
||||
});
|
||||
|
||||
const payload = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
throw new Error(payload?.message || payload?.errors?.message?.[0] || 'Message could not be sent.');
|
||||
}
|
||||
|
||||
if (payload.send_url) {
|
||||
root.dataset.sendUrl = payload.send_url;
|
||||
}
|
||||
|
||||
if (payload.read_url) {
|
||||
root.dataset.readUrl = payload.read_url;
|
||||
}
|
||||
|
||||
if (payload.conversation_id) {
|
||||
root.dataset.conversationId = String(payload.conversation_id);
|
||||
}
|
||||
|
||||
if (payload.message) {
|
||||
appendInlineChatMessage(thread, emptyState, payload.message);
|
||||
}
|
||||
|
||||
updateHeaderInboxBadge(payload?.counts?.unread_messages_total ?? 0);
|
||||
setUnreadCount(payload?.conversation?.unread_count ?? 0);
|
||||
input.value = '';
|
||||
setState('open');
|
||||
} catch (requestError) {
|
||||
showError(requestError instanceof Error ? requestError.message : 'Message could not be sent.');
|
||||
setState('open');
|
||||
} finally {
|
||||
submitButton.disabled = false;
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener('oc:inbox-message-created', async ({ detail }) => {
|
||||
if (String(detail?.conversation?.id || '') !== currentConversationId()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (root.dataset.state === 'open') {
|
||||
appendInlineChatMessage(thread, emptyState, detail.message);
|
||||
|
||||
if (!detail?.message?.is_mine && document.visibilityState === 'visible') {
|
||||
await markConversationRead();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!detail?.message?.is_mine) {
|
||||
setUnreadCount(detail?.conversation?.unread_count ?? 0);
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener('oc:inbox-read-updated', ({ detail }) => {
|
||||
if (String(detail?.conversation?.id || '') !== currentConversationId()) {
|
||||
return;
|
||||
}
|
||||
|
||||
setUnreadCount(detail?.conversation?.unread_count ?? 0);
|
||||
});
|
||||
|
||||
document.addEventListener('visibilitychange', async () => {
|
||||
if (document.visibilityState === 'visible' && root.dataset.state === 'open') {
|
||||
await markConversationRead();
|
||||
}
|
||||
});
|
||||
|
||||
setState('collapsed');
|
||||
};
|
||||
|
||||
const buildInboxMessageItem = (message) => {
|
||||
const wrapper = document.createElement('div');
|
||||
wrapper.dataset.messageId = String(message.id);
|
||||
wrapper.className = `mb-4 flex ${message.is_mine ? 'justify-end' : 'justify-start'}`;
|
||||
|
||||
const shell = document.createElement('div');
|
||||
shell.className = 'max-w-[80%]';
|
||||
|
||||
const bubble = document.createElement('div');
|
||||
bubble.className = `${message.is_mine ? 'bg-amber-100 text-slate-900' : 'bg-white text-slate-900 border border-slate-200'} rounded-2xl px-4 py-2 text-base shadow-sm`;
|
||||
bubble.textContent = message.body;
|
||||
|
||||
const time = document.createElement('p');
|
||||
time.className = `text-xs text-slate-500 mt-1 ${message.is_mine ? 'text-right' : 'text-left'}`;
|
||||
time.textContent = message.time || '';
|
||||
|
||||
shell.append(bubble, time);
|
||||
wrapper.appendChild(shell);
|
||||
|
||||
return wrapper;
|
||||
};
|
||||
|
||||
const initInboxRealtime = () => {
|
||||
const root = document.querySelector('[data-inbox-root]');
|
||||
|
||||
if (!root) {
|
||||
return;
|
||||
}
|
||||
|
||||
const listContainer = root.querySelector('[data-inbox-list-container]');
|
||||
const threadContainer = root.querySelector('[data-inbox-thread-container]');
|
||||
|
||||
if (!listContainer || !threadContainer) {
|
||||
return;
|
||||
}
|
||||
|
||||
const currentSelectedConversationId = () => {
|
||||
const panel = threadContainer.querySelector('[data-inbox-thread-panel]');
|
||||
|
||||
return String(
|
||||
root.dataset.selectedConversationId ||
|
||||
panel?.dataset.selectedConversationId ||
|
||||
'',
|
||||
).trim();
|
||||
};
|
||||
|
||||
const readUrlFor = (conversationId) => {
|
||||
const template = String(root.dataset.readUrlTemplate || '');
|
||||
|
||||
return conversationId && template ? template.replace('__CONVERSATION__', conversationId) : '';
|
||||
};
|
||||
|
||||
const currentFilter = () => {
|
||||
const params = new URLSearchParams(window.location.search);
|
||||
|
||||
return params.get('message_filter') || 'all';
|
||||
};
|
||||
|
||||
const syncBrowserUrl = (conversationId) => {
|
||||
const url = new URL(window.location.href);
|
||||
const filter = currentFilter();
|
||||
|
||||
if (filter === 'all') {
|
||||
url.searchParams.delete('message_filter');
|
||||
} else {
|
||||
url.searchParams.set('message_filter', filter);
|
||||
}
|
||||
|
||||
if (conversationId) {
|
||||
url.searchParams.set('conversation', conversationId);
|
||||
} else {
|
||||
url.searchParams.delete('conversation');
|
||||
}
|
||||
|
||||
window.history.replaceState({}, '', url);
|
||||
};
|
||||
|
||||
const refreshState = async ({ conversationId = currentSelectedConversationId(), scrollToBottom = false } = {}) => {
|
||||
const params = new URLSearchParams();
|
||||
|
||||
if (currentFilter() !== 'all') {
|
||||
params.set('message_filter', currentFilter());
|
||||
}
|
||||
|
||||
if (conversationId) {
|
||||
params.set('conversation', conversationId);
|
||||
}
|
||||
|
||||
const targetUrl = `${root.dataset.stateUrl}${params.toString() ? `?${params.toString()}` : ''}`;
|
||||
const response = await fetch(targetUrl, {
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return;
|
||||
}
|
||||
|
||||
const payload = await response.json().catch(() => null);
|
||||
if (!payload) {
|
||||
return;
|
||||
}
|
||||
|
||||
listContainer.innerHTML = payload.list_html || '';
|
||||
threadContainer.innerHTML = payload.thread_html || '';
|
||||
root.dataset.selectedConversationId = payload.selected_conversation_id ? String(payload.selected_conversation_id) : '';
|
||||
updateHeaderInboxBadge(payload?.counts?.unread_messages_total ?? 0);
|
||||
syncBrowserUrl(root.dataset.selectedConversationId);
|
||||
|
||||
if (scrollToBottom) {
|
||||
const thread = threadContainer.querySelector('[data-inbox-thread]');
|
||||
thread?.scrollTo({ top: thread.scrollHeight });
|
||||
}
|
||||
};
|
||||
|
||||
const showThreadError = (message) => {
|
||||
const error = threadContainer.querySelector('[data-inbox-send-error]');
|
||||
|
||||
if (!error) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!message) {
|
||||
error.textContent = '';
|
||||
error.classList.add('hidden');
|
||||
return;
|
||||
}
|
||||
|
||||
error.textContent = message;
|
||||
error.classList.remove('hidden');
|
||||
};
|
||||
|
||||
const appendInboxMessage = (message) => {
|
||||
const thread = threadContainer.querySelector('[data-inbox-thread]');
|
||||
const emptyState = threadContainer.querySelector('[data-inbox-empty]');
|
||||
|
||||
if (!thread || !message?.body || thread.querySelector(`[data-message-id="${message.id}"]`)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (emptyState) {
|
||||
emptyState.remove();
|
||||
}
|
||||
|
||||
thread.appendChild(buildInboxMessageItem(message));
|
||||
thread.scrollTop = thread.scrollHeight;
|
||||
};
|
||||
|
||||
const markConversationRead = async (conversationId) => {
|
||||
const readUrl = readUrlFor(conversationId);
|
||||
|
||||
if (!readUrl) {
|
||||
return;
|
||||
}
|
||||
|
||||
const response = await fetch(readUrl, {
|
||||
method: 'POST',
|
||||
headers: jsonHeaders(),
|
||||
});
|
||||
|
||||
const payload = await response.json().catch(() => null);
|
||||
|
||||
if (payload) {
|
||||
updateHeaderInboxBadge(payload?.counts?.unread_messages_total ?? 0);
|
||||
}
|
||||
};
|
||||
|
||||
root.addEventListener('submit', async (event) => {
|
||||
const form = event.target.closest('[data-inbox-send-form]');
|
||||
|
||||
if (!form) {
|
||||
return;
|
||||
}
|
||||
|
||||
event.preventDefault();
|
||||
|
||||
const formData = new FormData(form);
|
||||
const message = String(formData.get('message') || '').trim();
|
||||
const submitButton = form.querySelector('[data-inbox-send-button]') || form.querySelector('button[type="submit"]');
|
||||
const textInput = threadContainer.querySelector('[data-inbox-message-input]');
|
||||
|
||||
if (message === '') {
|
||||
showThreadError('Message cannot be empty.');
|
||||
textInput?.focus();
|
||||
return;
|
||||
}
|
||||
|
||||
showThreadError('');
|
||||
submitButton?.setAttribute('disabled', 'disabled');
|
||||
|
||||
try {
|
||||
const response = await fetch(form.action, {
|
||||
method: 'POST',
|
||||
headers: jsonHeaders(),
|
||||
body: new URLSearchParams(formData).toString(),
|
||||
});
|
||||
|
||||
const payload = await response.json().catch(() => ({}));
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(payload?.message || payload?.errors?.message?.[0] || 'Message could not be sent.');
|
||||
}
|
||||
|
||||
if (payload.message && String(payload.conversation_id || '') === currentSelectedConversationId()) {
|
||||
appendInboxMessage(payload.message);
|
||||
}
|
||||
|
||||
if (textInput && form.contains(textInput)) {
|
||||
textInput.value = '';
|
||||
textInput.focus();
|
||||
}
|
||||
|
||||
root.dataset.selectedConversationId = String(payload.conversation_id || currentSelectedConversationId());
|
||||
await refreshState({ conversationId: root.dataset.selectedConversationId, scrollToBottom: true });
|
||||
} catch (requestError) {
|
||||
showThreadError(requestError instanceof Error ? requestError.message : 'Message could not be sent.');
|
||||
} finally {
|
||||
submitButton?.removeAttribute('disabled');
|
||||
}
|
||||
});
|
||||
|
||||
document.addEventListener('oc:inbox-message-created', async ({ detail }) => {
|
||||
const selectedConversationId = currentSelectedConversationId();
|
||||
const eventConversationId = String(detail?.conversation?.id || '');
|
||||
|
||||
if (selectedConversationId && eventConversationId === selectedConversationId) {
|
||||
appendInboxMessage(detail.message);
|
||||
|
||||
if (!detail?.message?.is_mine && document.visibilityState === 'visible') {
|
||||
await markConversationRead(selectedConversationId);
|
||||
}
|
||||
|
||||
await refreshState({ conversationId: selectedConversationId, scrollToBottom: true });
|
||||
return;
|
||||
}
|
||||
|
||||
await refreshState({ conversationId: selectedConversationId });
|
||||
});
|
||||
|
||||
document.addEventListener('oc:inbox-read-updated', async () => {
|
||||
await refreshState({ conversationId: currentSelectedConversationId() });
|
||||
});
|
||||
|
||||
document.addEventListener('visibilitychange', async () => {
|
||||
const selectedConversationId = currentSelectedConversationId();
|
||||
|
||||
if (document.visibilityState !== 'visible' || !selectedConversationId) {
|
||||
return;
|
||||
}
|
||||
|
||||
await markConversationRead(selectedConversationId);
|
||||
await refreshState({ conversationId: selectedConversationId });
|
||||
});
|
||||
};
|
||||
|
||||
onReady(() => {
|
||||
subscribeInboxChannel();
|
||||
initInlineListingChat();
|
||||
initInboxRealtime();
|
||||
});
|
||||
@ -1,174 +1,57 @@
|
||||
@extends('app::layouts.app')
|
||||
|
||||
@section('title', 'Gelen Kutusu')
|
||||
@section('title', 'Inbox')
|
||||
|
||||
@section('content')
|
||||
<div class="max-w-[1320px] mx-auto px-4 py-8">
|
||||
<div class="grid grid-cols-1 lg:grid-cols-[220px,1fr] gap-4">
|
||||
@include('panel.partials.sidebar', ['activeMenu' => 'inbox'])
|
||||
@include('panel::partials.sidebar', ['activeMenu' => 'inbox'])
|
||||
|
||||
<section class="bg-white border border-slate-200 rounded-xl p-0 overflow-hidden">
|
||||
<section class="space-y-4">
|
||||
@include('panel::partials.page-header', [
|
||||
'title' => 'Inbox',
|
||||
'description' => 'Read and reply to buyer messages from the same panel shell used across the site.',
|
||||
'actions' => $requiresLogin ?? false
|
||||
? new \Illuminate\Support\HtmlString('<a href="' . e(route('login', ['redirect' => request()->fullUrl()])) . '" class="inline-flex items-center justify-center rounded-full bg-slate-900 px-5 py-3 text-sm font-semibold text-white transition hover:bg-slate-800">Log in</a>')
|
||||
: null,
|
||||
])
|
||||
|
||||
<div
|
||||
class="panel-surface overflow-hidden p-0"
|
||||
@auth
|
||||
data-inbox-root
|
||||
data-state-url="{{ route('panel.inbox.state') }}"
|
||||
data-read-url-template="{{ route('conversations.read', ['conversation' => '__CONVERSATION__']) }}"
|
||||
data-selected-conversation-id="{{ $selectedConversation?->id ?? '' }}"
|
||||
@endauth
|
||||
>
|
||||
@if($requiresLogin ?? false)
|
||||
<div class="border-b border-slate-200 px-5 py-4 bg-slate-50 flex flex-wrap items-center justify-between gap-3">
|
||||
<div class="border-b border-slate-200 px-5 py-4 bg-slate-50">
|
||||
<div>
|
||||
<h1 class="text-xl font-semibold text-slate-900">Inbox</h1>
|
||||
<p class="text-sm text-slate-500 mt-1">Stay on this page and log in when you want to access your conversations.</p>
|
||||
<p class="mt-1 text-sm text-slate-500">Stay on this page and log in when you want to access your conversations.</p>
|
||||
</div>
|
||||
<a href="{{ route('login', ['redirect' => request()->fullUrl()]) }}" class="inline-flex items-center justify-center rounded-full bg-slate-900 px-5 py-2.5 text-sm font-semibold text-white hover:bg-slate-800 transition">
|
||||
Log in
|
||||
</a>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<div class="grid grid-cols-1 xl:grid-cols-[420px,1fr] min-h-[620px]">
|
||||
<div class="border-b xl:border-b-0 xl:border-r border-slate-200">
|
||||
<div class="px-6 py-5 border-b border-slate-200 flex items-center justify-between gap-3">
|
||||
<h1 class="text-3xl font-bold text-slate-900">Gelen Kutusu</h1>
|
||||
<svg class="w-6 h-6 text-slate-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-4.35-4.35m1.6-5.05a7.25 7.25 0 11-14.5 0 7.25 7.25 0 0114.5 0z"/>
|
||||
</svg>
|
||||
</div>
|
||||
<div class="px-6 py-4 border-b border-slate-200">
|
||||
<p class="text-sm font-semibold text-slate-600 mb-2">Hızlı Filtreler</p>
|
||||
<div class="flex flex-wrap items-center gap-2">
|
||||
<a href="{{ route('panel.inbox.index', ['message_filter' => 'all']) }}" class="inline-flex items-center px-4 py-2 rounded-full text-sm font-semibold border {{ $messageFilter === 'all' ? 'border-rose-400 bg-rose-50 text-rose-600' : 'border-slate-300 text-slate-600 hover:bg-slate-100' }}">
|
||||
Hepsi
|
||||
</a>
|
||||
<a href="{{ route('panel.inbox.index', ['message_filter' => 'unread']) }}" class="inline-flex items-center px-4 py-2 rounded-full text-sm font-semibold border {{ $messageFilter === 'unread' ? 'border-rose-400 bg-rose-50 text-rose-600' : 'border-slate-300 text-slate-600 hover:bg-slate-100' }}">
|
||||
Okunmamış
|
||||
</a>
|
||||
<a href="{{ route('panel.inbox.index', ['message_filter' => 'important']) }}" class="inline-flex items-center px-4 py-2 rounded-full text-sm font-semibold border {{ $messageFilter === 'important' ? 'border-rose-400 bg-rose-50 text-rose-600' : 'border-slate-300 text-slate-600 hover:bg-slate-100' }}">
|
||||
Önemli
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="max-h-[480px] overflow-y-auto divide-y divide-slate-200">
|
||||
@forelse($conversations as $conversation)
|
||||
@php
|
||||
$conversationListing = $conversation->listing;
|
||||
$partner = (int) $conversation->buyer_id === (int) auth()->id() ? $conversation->seller : $conversation->buyer;
|
||||
$isSelected = $selectedConversation && (int) $selectedConversation->id === (int) $conversation->id;
|
||||
$conversationImage = $conversationListing?->getFirstMediaUrl('listing-images');
|
||||
$lastMessage = trim((string) ($conversation->lastMessage?->body ?? ''));
|
||||
@endphp
|
||||
<a href="{{ route('panel.inbox.index', ['message_filter' => $messageFilter, 'conversation' => $conversation->id]) }}" class="block px-6 py-4 transition {{ $isSelected ? 'bg-rose-50' : 'hover:bg-slate-50' }}">
|
||||
<div class="flex gap-3">
|
||||
<div class="w-14 h-14 rounded-xl bg-slate-100 border border-slate-200 overflow-hidden shrink-0">
|
||||
@if($conversationImage)
|
||||
<img src="{{ $conversationImage }}" alt="{{ $conversationListing?->title }}" class="w-full h-full object-cover">
|
||||
@else
|
||||
<div class="w-full h-full grid place-items-center text-slate-400 text-xs">İlan</div>
|
||||
@endif
|
||||
</div>
|
||||
<div class="min-w-0 flex-1">
|
||||
<div class="flex items-start gap-2">
|
||||
<p class="font-semibold text-2xl text-slate-900 truncate">{{ $partner?->name ?? 'Kullanıcı' }}</p>
|
||||
<p class="text-xs text-slate-500 whitespace-nowrap ml-auto">{{ $conversation->last_message_at?->format('d.m.Y') }}</p>
|
||||
</div>
|
||||
<p class="text-sm text-slate-500 truncate mt-1">{{ $conversationListing?->title ?? 'İlan silinmiş' }}</p>
|
||||
<p class="text-sm {{ $conversation->unread_count > 0 ? 'text-slate-900 font-semibold' : 'text-slate-500' }} truncate mt-1">
|
||||
{{ $lastMessage !== '' ? $lastMessage : 'Henüz mesaj yok' }}
|
||||
</p>
|
||||
</div>
|
||||
@if($conversation->unread_count > 0)
|
||||
<span class="inline-flex items-center justify-center min-w-6 h-6 px-2 rounded-full bg-rose-500 text-white text-xs font-semibold">
|
||||
{{ $conversation->unread_count }}
|
||||
</span>
|
||||
@endif
|
||||
</div>
|
||||
</a>
|
||||
@empty
|
||||
<div class="px-6 py-16 text-center text-slate-500">
|
||||
Henüz bir sohbetin yok.
|
||||
</div>
|
||||
@endforelse
|
||||
</div>
|
||||
<div data-inbox-list-container>
|
||||
@include('conversation::partials.inbox-list-pane', [
|
||||
'conversations' => $conversations,
|
||||
'messageFilter' => $messageFilter,
|
||||
'selectedConversation' => $selectedConversation,
|
||||
])
|
||||
</div>
|
||||
|
||||
<div class="flex flex-col min-h-[620px]">
|
||||
@if($selectedConversation)
|
||||
@php
|
||||
$activeListing = $selectedConversation->listing;
|
||||
$activePartner = (int) $selectedConversation->buyer_id === (int) auth()->id()
|
||||
? $selectedConversation->seller
|
||||
: $selectedConversation->buyer;
|
||||
$activePriceLabel = $activeListing && !is_null($activeListing->price)
|
||||
? number_format((float) $activeListing->price, 0).' '.($activeListing->currency ?? 'TL')
|
||||
: null;
|
||||
@endphp
|
||||
<div class="h-24 px-6 border-b border-slate-200 flex items-center gap-4">
|
||||
<div class="w-12 h-12 rounded-full bg-slate-600 text-white grid place-items-center font-semibold text-lg">
|
||||
{{ strtoupper(substr((string) ($activePartner?->name ?? 'K'), 0, 1)) }}
|
||||
</div>
|
||||
<div class="min-w-0">
|
||||
<p class="text-3xl font-bold text-slate-900 truncate">{{ $activePartner?->name ?? 'Kullanıcı' }}</p>
|
||||
<p class="text-sm text-slate-500 truncate">{{ $activeListing?->title ?? 'İlan silinmiş' }}</p>
|
||||
</div>
|
||||
@if($activePriceLabel)
|
||||
<div class="ml-auto text-3xl font-semibold text-slate-800 whitespace-nowrap">{{ $activePriceLabel }}</div>
|
||||
@endif
|
||||
</div>
|
||||
|
||||
<div class="flex-1 px-6 py-6 bg-slate-100/60 overflow-y-auto max-h-[390px]">
|
||||
@forelse($selectedConversation->messages as $message)
|
||||
@php $isMine = (int) $message->sender_id === (int) auth()->id(); @endphp
|
||||
<div class="mb-4 flex {{ $isMine ? 'justify-end' : 'justify-start' }}">
|
||||
<div class="max-w-[80%]">
|
||||
<div class="{{ $isMine ? 'bg-amber-100 text-slate-900' : 'bg-white text-slate-900 border border-slate-200' }} rounded-2xl px-4 py-2 text-base shadow-sm">
|
||||
{{ $message->body }}
|
||||
</div>
|
||||
<p class="text-xs text-slate-500 mt-1 {{ $isMine ? 'text-right' : 'text-left' }}">
|
||||
{{ $message->created_at?->format('H:i') }}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
@empty
|
||||
<div class="h-full grid place-items-center text-slate-500 text-center px-8">
|
||||
<div>
|
||||
<p class="font-semibold text-slate-700">Henüz mesaj yok.</p>
|
||||
<p class="text-sm mt-1">Aşağıdaki hazır metinlerden birini seçebilir veya yeni mesaj yazabilirsin.</p>
|
||||
</div>
|
||||
</div>
|
||||
@endforelse
|
||||
</div>
|
||||
|
||||
<div class="px-4 py-3 border-t border-slate-200 bg-white">
|
||||
<div class="flex items-center gap-2 overflow-x-auto pb-2">
|
||||
@foreach($quickMessages as $quickMessage)
|
||||
<form method="POST" action="{{ route('conversations.messages.send', $selectedConversation) }}" class="shrink-0">
|
||||
@csrf
|
||||
<input type="hidden" name="message_filter" value="{{ $messageFilter }}">
|
||||
<input type="hidden" name="message" value="{{ $quickMessage }}">
|
||||
<button type="submit" class="inline-flex items-center h-11 px-5 rounded-full border border-rose-300 text-rose-600 font-semibold text-sm hover:bg-rose-50 transition">
|
||||
{{ $quickMessage }}
|
||||
</button>
|
||||
</form>
|
||||
@endforeach
|
||||
</div>
|
||||
<form method="POST" action="{{ route('conversations.messages.send', $selectedConversation) }}" class="flex items-center gap-2 border-t border-slate-200 pt-3 mt-1">
|
||||
@csrf
|
||||
<input type="hidden" name="message_filter" value="{{ $messageFilter }}">
|
||||
<input type="text" name="message" value="{{ old('message') }}" placeholder="Bir mesaj yaz" maxlength="2000" class="h-12 flex-1 rounded-full border border-slate-300 px-5 text-sm focus:outline-none focus:ring-2 focus:ring-rose-300" required>
|
||||
<button type="submit" class="h-12 w-12 rounded-full bg-black text-white grid place-items-center hover:bg-slate-800 transition" aria-label="Gönder">
|
||||
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 12h13m0 0l-5-5m5 5l-5 5"/>
|
||||
</svg>
|
||||
</button>
|
||||
</form>
|
||||
@error('message')
|
||||
<p class="text-xs text-rose-600 mt-2 px-2">{{ $message }}</p>
|
||||
@enderror
|
||||
</div>
|
||||
@else
|
||||
<div class="h-full min-h-[620px] grid place-items-center px-8 text-center text-slate-500">
|
||||
<div>
|
||||
<p class="text-2xl font-semibold text-slate-700">Mesajlaşma için bir sohbet seç.</p>
|
||||
<p class="mt-2 text-sm">İlan detayından veya ilan kartlarından yeni sohbet başlatabilirsin.</p>
|
||||
</div>
|
||||
</div>
|
||||
@endif
|
||||
<div data-inbox-thread-container>
|
||||
@include('conversation::partials.inbox-thread-pane', [
|
||||
'selectedConversation' => $selectedConversation,
|
||||
'messageFilter' => $messageFilter,
|
||||
'quickMessages' => $quickMessages,
|
||||
])
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -0,0 +1,61 @@
|
||||
<div class="border-b xl:border-b-0 xl:border-r border-slate-200">
|
||||
<div class="px-6 py-4 border-b border-slate-200">
|
||||
<p class="mb-2 text-sm font-semibold text-slate-600">Filters</p>
|
||||
<div class="flex flex-wrap items-center gap-2">
|
||||
<a href="{{ route('panel.inbox.index', ['message_filter' => 'all']) }}" class="inline-flex items-center px-4 py-2 rounded-full text-sm font-semibold border {{ $messageFilter === 'all' ? 'border-rose-400 bg-rose-50 text-rose-600' : 'border-slate-300 text-slate-600 hover:bg-slate-100' }}">
|
||||
All
|
||||
</a>
|
||||
<a href="{{ route('panel.inbox.index', ['message_filter' => 'unread']) }}" class="inline-flex items-center px-4 py-2 rounded-full text-sm font-semibold border {{ $messageFilter === 'unread' ? 'border-rose-400 bg-rose-50 text-rose-600' : 'border-slate-300 text-slate-600 hover:bg-slate-100' }}">
|
||||
Unread
|
||||
</a>
|
||||
<a href="{{ route('panel.inbox.index', ['message_filter' => 'important']) }}" class="inline-flex items-center px-4 py-2 rounded-full text-sm font-semibold border {{ $messageFilter === 'important' ? 'border-rose-400 bg-rose-50 text-rose-600' : 'border-slate-300 text-slate-600 hover:bg-slate-100' }}">
|
||||
Important
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="max-h-[480px] overflow-y-auto divide-y divide-slate-200">
|
||||
@forelse($conversations as $conversation)
|
||||
@php
|
||||
$conversationListing = $conversation->listing;
|
||||
$partner = (int) $conversation->buyer_id === (int) auth()->id() ? $conversation->seller : $conversation->buyer;
|
||||
$isSelected = $selectedConversation && (int) $selectedConversation->id === (int) $conversation->id;
|
||||
$conversationImage = $conversationListing?->primaryImageData('thumb');
|
||||
$lastMessage = trim((string) ($conversation->lastMessage?->body ?? ''));
|
||||
@endphp
|
||||
<a href="{{ route('panel.inbox.index', ['message_filter' => $messageFilter, 'conversation' => $conversation->id]) }}" class="block px-6 py-4 transition {{ $isSelected ? 'bg-rose-50' : 'hover:bg-slate-50' }}">
|
||||
<div class="flex gap-3">
|
||||
<div class="w-14 h-14 rounded-xl bg-slate-100 border border-slate-200 overflow-hidden shrink-0">
|
||||
@if($conversationImage)
|
||||
@include('listing::partials.responsive-image', [
|
||||
'image' => $conversationImage,
|
||||
'alt' => $conversationListing?->title,
|
||||
'class' => 'w-full h-full object-cover',
|
||||
])
|
||||
@else
|
||||
<div class="w-full h-full grid place-items-center text-slate-400 text-xs">Listing</div>
|
||||
@endif
|
||||
</div>
|
||||
<div class="min-w-0 flex-1">
|
||||
<div class="flex items-start gap-2">
|
||||
<p class="font-semibold text-2xl text-slate-900 truncate">{{ $partner?->name ?? 'User' }}</p>
|
||||
<p class="text-xs text-slate-500 whitespace-nowrap ml-auto">{{ $conversation->last_message_at?->format('d.m.Y') }}</p>
|
||||
</div>
|
||||
<p class="text-sm text-slate-500 truncate mt-1">{{ $conversationListing?->title ?? 'Listing removed' }}</p>
|
||||
<p class="text-sm {{ $conversation->unread_count > 0 ? 'text-slate-900 font-semibold' : 'text-slate-500' }} truncate mt-1">
|
||||
{{ $lastMessage !== '' ? $lastMessage : 'No messages yet' }}
|
||||
</p>
|
||||
</div>
|
||||
@if($conversation->unread_count > 0)
|
||||
<span class="inline-flex items-center justify-center min-w-6 h-6 px-2 rounded-full bg-rose-500 text-white text-xs font-semibold">
|
||||
{{ $conversation->unread_count }}
|
||||
</span>
|
||||
@endif
|
||||
</div>
|
||||
</a>
|
||||
@empty
|
||||
<div class="px-6 py-16 text-center text-slate-500">
|
||||
No conversations yet.
|
||||
</div>
|
||||
@endforelse
|
||||
</div>
|
||||
</div>
|
||||
@ -0,0 +1,81 @@
|
||||
<div class="flex flex-col min-h-[620px]" data-inbox-thread-panel data-selected-conversation-id="{{ $selectedConversation?->id ?? '' }}">
|
||||
@if($selectedConversation)
|
||||
@php
|
||||
$activeListing = $selectedConversation->listing;
|
||||
$activePartner = (int) $selectedConversation->buyer_id === (int) auth()->id()
|
||||
? $selectedConversation->seller
|
||||
: $selectedConversation->buyer;
|
||||
$activePriceLabel = $activeListing && !is_null($activeListing->price)
|
||||
? number_format((float) $activeListing->price, 0).' '.($activeListing->currency ?? 'TL')
|
||||
: null;
|
||||
@endphp
|
||||
<div class="h-24 px-6 border-b border-slate-200 flex items-center gap-4">
|
||||
<div class="w-12 h-12 rounded-full bg-slate-600 text-white grid place-items-center font-semibold text-lg">
|
||||
{{ strtoupper(substr((string) ($activePartner?->name ?? 'K'), 0, 1)) }}
|
||||
</div>
|
||||
<div class="min-w-0">
|
||||
<p class="text-3xl font-bold text-slate-900 truncate">{{ $activePartner?->name ?? 'User' }}</p>
|
||||
<p class="text-sm text-slate-500 truncate">{{ $activeListing?->title ?? 'Listing removed' }}</p>
|
||||
</div>
|
||||
@if($activePriceLabel)
|
||||
<div class="ml-auto text-3xl font-semibold text-slate-800 whitespace-nowrap">{{ $activePriceLabel }}</div>
|
||||
@endif
|
||||
</div>
|
||||
|
||||
<div class="flex-1 px-6 py-6 bg-slate-100/60 overflow-y-auto max-h-[390px]" data-inbox-thread>
|
||||
@forelse($selectedConversation->messages as $message)
|
||||
@php $isMine = (int) $message->sender_id === (int) auth()->id(); @endphp
|
||||
<div class="mb-4 flex {{ $isMine ? 'justify-end' : 'justify-start' }}" data-message-id="{{ $message->id }}">
|
||||
<div class="max-w-[80%]">
|
||||
<div class="{{ $isMine ? 'bg-amber-100 text-slate-900' : 'bg-white text-slate-900 border border-slate-200' }} rounded-2xl px-4 py-2 text-base shadow-sm">
|
||||
{{ $message->body }}
|
||||
</div>
|
||||
<p class="text-xs text-slate-500 mt-1 {{ $isMine ? 'text-right' : 'text-left' }}">
|
||||
{{ $message->created_at?->format('H:i') }}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
@empty
|
||||
<div class="h-full grid place-items-center text-slate-500 text-center px-8" data-inbox-empty>
|
||||
<div>
|
||||
<p class="font-semibold text-slate-700">No messages yet.</p>
|
||||
<p class="mt-1 text-sm">Use a quick reply or send the first message below.</p>
|
||||
</div>
|
||||
</div>
|
||||
@endforelse
|
||||
</div>
|
||||
|
||||
<div class="px-4 py-3 border-t border-slate-200 bg-white">
|
||||
<div class="flex items-center gap-2 overflow-x-auto pb-2">
|
||||
@foreach($quickMessages as $quickMessage)
|
||||
<form method="POST" action="{{ route('conversations.messages.send', $selectedConversation) }}" class="shrink-0" data-inbox-send-form>
|
||||
@csrf
|
||||
<input type="hidden" name="message_filter" value="{{ $messageFilter }}">
|
||||
<input type="hidden" name="message" value="{{ $quickMessage }}">
|
||||
<button type="submit" class="inline-flex items-center h-11 px-5 rounded-full border border-rose-300 text-rose-600 font-semibold text-sm hover:bg-rose-50 transition">
|
||||
{{ $quickMessage }}
|
||||
</button>
|
||||
</form>
|
||||
@endforeach
|
||||
</div>
|
||||
<form method="POST" action="{{ route('conversations.messages.send', $selectedConversation) }}" class="flex items-center gap-2 border-t border-slate-200 pt-3 mt-1" data-inbox-send-form>
|
||||
@csrf
|
||||
<input type="hidden" name="message_filter" value="{{ $messageFilter }}">
|
||||
<input type="text" name="message" value="{{ old('message') }}" placeholder="Write a message" maxlength="2000" class="h-12 flex-1 rounded-full border border-slate-300 px-5 text-sm focus:outline-none focus:ring-2 focus:ring-rose-300" required data-inbox-message-input>
|
||||
<button type="submit" class="h-12 w-12 rounded-full bg-black text-white grid place-items-center hover:bg-slate-800 transition" aria-label="Send" data-inbox-send-button>
|
||||
<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 12h13m0 0l-5-5m5 5l-5 5"/>
|
||||
</svg>
|
||||
</button>
|
||||
</form>
|
||||
<p class="text-xs text-rose-600 mt-2 px-2 hidden" data-inbox-send-error></p>
|
||||
</div>
|
||||
@else
|
||||
<div class="h-full min-h-[620px] grid place-items-center px-8 text-center text-slate-500">
|
||||
<div>
|
||||
<p class="text-2xl font-semibold text-slate-700">Choose a conversation to start messaging.</p>
|
||||
<p class="mt-2 text-sm">Start a new chat from a listing detail page or continue an existing thread here.</p>
|
||||
</div>
|
||||
</div>
|
||||
@endif
|
||||
</div>
|
||||
@ -3,11 +3,15 @@
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Modules\Conversation\App\Http\Controllers\ConversationController;
|
||||
|
||||
Route::prefix('panel')->name('panel.')->group(function () {
|
||||
Route::get('/inbox', [ConversationController::class, 'inbox'])->name('inbox.index');
|
||||
});
|
||||
Route::middleware('web')->group(function () {
|
||||
Route::prefix('panel')->name('panel.')->group(function () {
|
||||
Route::get('/inbox', [ConversationController::class, 'inbox'])->name('inbox.index');
|
||||
Route::middleware('auth')->get('/inbox/state', [ConversationController::class, 'state'])->name('inbox.state');
|
||||
});
|
||||
|
||||
Route::middleware('auth')->name('conversations.')->group(function () {
|
||||
Route::post('/listings/{listing}/conversation', [ConversationController::class, 'start'])->name('start');
|
||||
Route::post('/conversations/{conversation}/messages', [ConversationController::class, 'send'])->name('messages.send');
|
||||
Route::middleware('auth')->name('conversations.')->group(function () {
|
||||
Route::post('/listings/{listing}/conversation', [ConversationController::class, 'start'])->name('start');
|
||||
Route::post('/conversations/{conversation}/messages', [ConversationController::class, 'send'])->name('messages.send');
|
||||
Route::post('/conversations/{conversation}/read', [ConversationController::class, 'read'])->name('read');
|
||||
});
|
||||
});
|
||||
|
||||
@ -8,11 +8,16 @@ use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Cookie;
|
||||
use Modules\Demo\App\Support\DemoSchemaManager;
|
||||
use Modules\Demo\App\Support\TurnstileVerifier;
|
||||
use Throwable;
|
||||
|
||||
class DemoController extends Controller
|
||||
{
|
||||
public function prepare(Request $request, DemoSchemaManager $demoSchemaManager): RedirectResponse
|
||||
public function prepare(
|
||||
Request $request,
|
||||
DemoSchemaManager $demoSchemaManager,
|
||||
TurnstileVerifier $turnstileVerifier,
|
||||
): RedirectResponse
|
||||
{
|
||||
abort_unless(config('demo.enabled'), 404);
|
||||
|
||||
@ -20,6 +25,29 @@ class DemoController extends Controller
|
||||
$redirectTo = $this->sanitizeRedirectTarget($request->input('redirect_to'))
|
||||
?? route('home');
|
||||
|
||||
if ($turnstileVerifier->enabled() && ! $turnstileVerifier->configured()) {
|
||||
return redirect()
|
||||
->to($redirectTo)
|
||||
->with('error', 'Security verification is unavailable right now. Please contact support.');
|
||||
}
|
||||
|
||||
if (! $turnstileVerifier->verify(
|
||||
$request->input('cf-turnstile-response'),
|
||||
$request->ip(),
|
||||
)) {
|
||||
return redirect()
|
||||
->to($redirectTo)
|
||||
->with('error', 'Security verification failed. Please complete the check and try again.');
|
||||
}
|
||||
|
||||
if (function_exists('set_time_limit')) {
|
||||
@set_time_limit(300);
|
||||
}
|
||||
|
||||
if (function_exists('ignore_user_abort')) {
|
||||
@ignore_user_abort(true);
|
||||
}
|
||||
|
||||
try {
|
||||
$instance = $demoSchemaManager->prepare($request->cookie($cookieName));
|
||||
$user = $demoSchemaManager->resolveLoginUser();
|
||||
|
||||
@ -25,7 +25,7 @@ class DemoServiceProvider extends ServiceProvider
|
||||
public function boot(): void
|
||||
{
|
||||
$this->guardConfiguration();
|
||||
$this->loadMigrationsFrom(module_path('Demo', 'database/migrations'));
|
||||
$this->loadMigrationsFrom(module_path('Demo', 'Database/migrations'));
|
||||
$this->loadRoutesFrom(module_path('Demo', 'routes/web.php'));
|
||||
}
|
||||
|
||||
|
||||
@ -2,11 +2,11 @@
|
||||
|
||||
namespace Modules\Demo\App\Support;
|
||||
|
||||
use App\Settings\GeneralSettings;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Modules\Demo\App\Models\DemoInstance;
|
||||
use Modules\Site\App\Settings\GeneralSettings;
|
||||
use Modules\User\App\Models\User;
|
||||
use Spatie\Permission\PermissionRegistrar;
|
||||
use Throwable;
|
||||
|
||||
72
Modules/Demo/App/Support/TurnstileVerifier.php
Normal file
72
Modules/Demo/App/Support/TurnstileVerifier.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Demo\App\Support;
|
||||
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Throwable;
|
||||
|
||||
final class TurnstileVerifier
|
||||
{
|
||||
public function enabled(): bool
|
||||
{
|
||||
return (bool) config('demo.turnstile.enabled', false);
|
||||
}
|
||||
|
||||
public function siteKey(): string
|
||||
{
|
||||
return trim((string) config('demo.turnstile.site_key', ''));
|
||||
}
|
||||
|
||||
public function configured(): bool
|
||||
{
|
||||
return $this->siteKey() !== '' && $this->secretKey() !== '';
|
||||
}
|
||||
|
||||
public function verify(?string $token, ?string $ip = null): bool
|
||||
{
|
||||
if (! $this->enabled()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (! $this->configured()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$token = trim((string) $token);
|
||||
|
||||
if ($token === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
$payload = [
|
||||
'secret' => $this->secretKey(),
|
||||
'response' => $token,
|
||||
];
|
||||
|
||||
$ip = trim((string) $ip);
|
||||
|
||||
if ($ip !== '') {
|
||||
$payload['remoteip'] = $ip;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = Http::asForm()
|
||||
->acceptJson()
|
||||
->timeout(max(3, (int) config('demo.turnstile.timeout_seconds', 8)))
|
||||
->post((string) config('demo.turnstile.verify_url'), $payload);
|
||||
|
||||
if (! $response->ok()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) data_get($response->json(), 'success', false);
|
||||
} catch (Throwable) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private function secretKey(): string
|
||||
{
|
||||
return trim((string) config('demo.turnstile.secret_key', ''));
|
||||
}
|
||||
}
|
||||
15
Modules/Demo/Database/Seeders/DemoContentSeeder.php
Normal file
15
Modules/Demo/Database/Seeders/DemoContentSeeder.php
Normal file
@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Demo\Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class DemoContentSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$this->call([
|
||||
\Modules\User\Database\Seeders\UserWorkspaceSeeder::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@ -1,47 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Demo\Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Modules\User\App\Models\User;
|
||||
use Spatie\Permission\Models\Role;
|
||||
|
||||
class DemoContentSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$admin = User::query()->updateOrCreate(
|
||||
['email' => 'a@a.com'],
|
||||
[
|
||||
'name' => 'Admin',
|
||||
'password' => Hash::make('236330'),
|
||||
'status' => 'active',
|
||||
],
|
||||
);
|
||||
|
||||
$partner = User::query()->updateOrCreate(
|
||||
['email' => 'b@b.com'],
|
||||
[
|
||||
'name' => 'Partner',
|
||||
'password' => Hash::make('36330'),
|
||||
'status' => 'active',
|
||||
],
|
||||
);
|
||||
|
||||
if (class_exists(Role::class)) {
|
||||
$adminRole = Role::firstOrCreate(['name' => 'admin', 'guard_name' => 'web']);
|
||||
$partnerRole = Role::firstOrCreate(['name' => 'partner', 'guard_name' => 'web']);
|
||||
|
||||
$admin->syncRoles([$adminRole->name]);
|
||||
$partner->syncRoles([$partnerRole->name]);
|
||||
}
|
||||
|
||||
$this->call([
|
||||
\Modules\Listing\Database\Seeders\ListingSeeder::class,
|
||||
\Modules\Listing\Database\Seeders\ListingPanelDemoSeeder::class,
|
||||
\Modules\Favorite\Database\Seeders\FavoriteDemoSeeder::class,
|
||||
\Modules\Conversation\Database\Seeders\ConversationDemoSeeder::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@ -4,5 +4,7 @@ use Illuminate\Support\Facades\Route;
|
||||
use Modules\Demo\App\Http\Controllers\DemoController;
|
||||
|
||||
Route::middleware('web')->group(function () {
|
||||
Route::post('/demo/prepare', [DemoController::class, 'prepare'])->name('demo.prepare');
|
||||
Route::post('/demo/prepare', [DemoController::class, 'prepare'])
|
||||
->middleware('throttle:8,1')
|
||||
->name('demo.prepare');
|
||||
});
|
||||
|
||||
@ -5,16 +5,19 @@ namespace Modules\Favorite\App\Http\Controllers;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Modules\Category\Models\Category;
|
||||
use Modules\Conversation\App\Models\Conversation;
|
||||
use Modules\Favorite\App\Models\FavoriteSearch;
|
||||
use Modules\Listing\Models\Listing;
|
||||
use Modules\User\App\Models\User;
|
||||
use Throwable;
|
||||
use Modules\User\App\Support\AuthRedirector;
|
||||
|
||||
class FavoriteController extends Controller
|
||||
{
|
||||
public function __construct(private AuthRedirector $redirector)
|
||||
{
|
||||
}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$activeTab = (string) $request->string('tab', 'listings');
|
||||
@ -35,13 +38,7 @@ class FavoriteController extends Controller
|
||||
$user = $request->user();
|
||||
$requiresLogin = ! $user;
|
||||
|
||||
$categories = collect();
|
||||
if ($this->tableExists('categories')) {
|
||||
$categories = Category::query()
|
||||
->where('is_active', true)
|
||||
->orderBy('name')
|
||||
->get(['id', 'name']);
|
||||
}
|
||||
$categories = Category::filterOptions();
|
||||
|
||||
$favoriteListings = $this->emptyPaginator();
|
||||
$favoriteSearches = $this->emptyPaginator();
|
||||
@ -49,64 +46,22 @@ class FavoriteController extends Controller
|
||||
$buyerConversationListingMap = [];
|
||||
|
||||
if ($user && $activeTab === 'listings') {
|
||||
try {
|
||||
if ($this->tableExists('favorite_listings')) {
|
||||
$favoriteListings = $user->favoriteListings()
|
||||
->with(['category:id,name', 'user:id,name'])
|
||||
->wherePivot('created_at', '>=', now()->subYear())
|
||||
->when($statusFilter === 'active', fn ($query) => $query->where('status', 'active'))
|
||||
->when($selectedCategoryId, fn ($query) => $query->where('category_id', $selectedCategoryId))
|
||||
->orderByPivot('created_at', 'desc')
|
||||
->paginate(10)
|
||||
->withQueryString();
|
||||
}
|
||||
$favoriteListings = $user->favoriteListingsPage($statusFilter, $selectedCategoryId);
|
||||
|
||||
if (
|
||||
$favoriteListings->isNotEmpty()
|
||||
&& $this->tableExists('conversations')
|
||||
) {
|
||||
$userId = (int) $user->getKey();
|
||||
$buyerConversationListingMap = Conversation::query()
|
||||
->where('buyer_id', $userId)
|
||||
->whereIn('listing_id', $favoriteListings->pluck('id')->all())
|
||||
->pluck('id', 'listing_id')
|
||||
->map(fn ($conversationId) => (int) $conversationId)
|
||||
->all();
|
||||
}
|
||||
} catch (Throwable) {
|
||||
$favoriteListings = $this->emptyPaginator();
|
||||
$buyerConversationListingMap = [];
|
||||
if ($favoriteListings->isNotEmpty()) {
|
||||
$buyerConversationListingMap = Conversation::listingMapForBuyer(
|
||||
(int) $user->getKey(),
|
||||
$favoriteListings->pluck('id')->all(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ($user && $activeTab === 'searches') {
|
||||
try {
|
||||
if ($this->tableExists('favorite_searches')) {
|
||||
$favoriteSearches = $user->favoriteSearches()
|
||||
->with('category:id,name')
|
||||
->latest()
|
||||
->paginate(10)
|
||||
->withQueryString();
|
||||
}
|
||||
} catch (Throwable) {
|
||||
$favoriteSearches = $this->emptyPaginator();
|
||||
}
|
||||
$favoriteSearches = $user->favoriteSearchesPage();
|
||||
}
|
||||
|
||||
if ($user && $activeTab === 'sellers') {
|
||||
try {
|
||||
if ($this->tableExists('favorite_sellers')) {
|
||||
$favoriteSellers = $user->favoriteSellers()
|
||||
->withCount([
|
||||
'listings as active_listings_count' => fn ($query) => $query->where('status', 'active'),
|
||||
])
|
||||
->orderByPivot('created_at', 'desc')
|
||||
->paginate(10)
|
||||
->withQueryString();
|
||||
}
|
||||
} catch (Throwable) {
|
||||
$favoriteSellers = $this->emptyPaginator();
|
||||
}
|
||||
$favoriteSellers = $user->favoriteSellersPage();
|
||||
}
|
||||
|
||||
return view('favorite::index', [
|
||||
@ -126,7 +81,7 @@ class FavoriteController extends Controller
|
||||
{
|
||||
$isNowFavorite = $request->user()->toggleFavoriteListing($listing);
|
||||
|
||||
return back()->with('success', $isNowFavorite ? 'İlan favorilere eklendi.' : 'İlan favorilerden kaldırıldı.');
|
||||
return $this->redirectBack($request)->with('success', $isNowFavorite ? 'Listing added to favorites.' : 'Listing removed from favorites.');
|
||||
}
|
||||
|
||||
public function toggleSeller(Request $request, User $seller)
|
||||
@ -134,12 +89,12 @@ class FavoriteController extends Controller
|
||||
$user = $request->user();
|
||||
|
||||
if ((int) $user->getKey() === (int) $seller->getKey()) {
|
||||
return back()->with('error', 'Kendi hesabını favorilere ekleyemezsin.');
|
||||
return $this->redirectBack($request)->with('error', 'You cannot favorite your own account.');
|
||||
}
|
||||
|
||||
$isNowFavorite = $user->toggleFavoriteSeller($seller);
|
||||
|
||||
return back()->with('success', $isNowFavorite ? 'Satıcı favorilere eklendi.' : 'Satıcı favorilerden kaldırıldı.');
|
||||
return $this->redirectBack($request)->with('success', $isNowFavorite ? 'Seller added to favorites.' : 'Seller removed from favorites.');
|
||||
}
|
||||
|
||||
public function storeSearch(Request $request)
|
||||
@ -155,33 +110,16 @@ class FavoriteController extends Controller
|
||||
]);
|
||||
|
||||
if ($filters === []) {
|
||||
return back()->with('error', 'Favoriye eklemek için en az bir filtre seçmelisin.');
|
||||
return back()->with('error', 'Select at least one filter before saving a search.');
|
||||
}
|
||||
|
||||
$signature = FavoriteSearch::signatureFor($filters);
|
||||
|
||||
$categoryName = null;
|
||||
if (isset($filters['category'])) {
|
||||
$categoryName = Category::query()->whereKey($filters['category'])->value('name');
|
||||
}
|
||||
|
||||
$label = FavoriteSearch::labelFor($filters, is_string($categoryName) ? $categoryName : null);
|
||||
|
||||
$favoriteSearch = $request->user()->favoriteSearches()->firstOrCreate(
|
||||
['signature' => $signature],
|
||||
[
|
||||
'label' => $label,
|
||||
'search_term' => $filters['search'] ?? null,
|
||||
'category_id' => $filters['category'] ?? null,
|
||||
'filters' => $filters,
|
||||
]
|
||||
);
|
||||
$favoriteSearch = FavoriteSearch::storeForUser($request->user(), $filters);
|
||||
|
||||
if (! $favoriteSearch->wasRecentlyCreated) {
|
||||
return back()->with('success', 'Bu arama zaten favorilerinde.');
|
||||
return back()->with('success', 'This search is already in your favorites.');
|
||||
}
|
||||
|
||||
return back()->with('success', 'Arama favorilere eklendi.');
|
||||
return back()->with('success', 'Search added to favorites.');
|
||||
}
|
||||
|
||||
public function destroySearch(Request $request, FavoriteSearch $favoriteSearch)
|
||||
@ -192,16 +130,7 @@ class FavoriteController extends Controller
|
||||
|
||||
$favoriteSearch->delete();
|
||||
|
||||
return back()->with('success', 'Favori arama silindi.');
|
||||
}
|
||||
|
||||
private function tableExists(string $table): bool
|
||||
{
|
||||
try {
|
||||
return Schema::hasTable($table);
|
||||
} catch (Throwable) {
|
||||
return false;
|
||||
}
|
||||
return back()->with('success', 'Saved search deleted.');
|
||||
}
|
||||
|
||||
private function emptyPaginator(): LengthAwarePaginator
|
||||
@ -211,4 +140,15 @@ class FavoriteController extends Controller
|
||||
'query' => request()->query(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function redirectBack(Request $request): \Illuminate\Http\RedirectResponse
|
||||
{
|
||||
$target = $this->redirector->sanitize((string) $request->input('redirect_to', ''));
|
||||
|
||||
if ($target !== null) {
|
||||
return redirect()->to($target);
|
||||
}
|
||||
|
||||
return back();
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,6 +51,38 @@ class FavoriteSearch extends Model
|
||||
$labelParts[] = $categoryName;
|
||||
}
|
||||
|
||||
return $labelParts !== [] ? implode(' · ', $labelParts) : 'Filtreli arama';
|
||||
return $labelParts !== [] ? implode(' · ', $labelParts) : 'Filtered search';
|
||||
}
|
||||
|
||||
public static function isSavedForUser(User $user, array $filters): bool
|
||||
{
|
||||
$normalized = static::normalizeFilters($filters);
|
||||
|
||||
if ($normalized === []) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $user->favoriteSearches()
|
||||
->where('signature', static::signatureFor($normalized))
|
||||
->exists();
|
||||
}
|
||||
|
||||
public static function storeForUser(User $user, array $filters): self
|
||||
{
|
||||
$normalized = static::normalizeFilters($filters);
|
||||
$signature = static::signatureFor($normalized);
|
||||
$categoryName = isset($normalized['category'])
|
||||
? Category::query()->whereKey($normalized['category'])->value('name')
|
||||
: null;
|
||||
|
||||
return $user->favoriteSearches()->firstOrCreate(
|
||||
['signature' => $signature],
|
||||
[
|
||||
'label' => static::labelFor($normalized, is_string($categoryName) ? $categoryName : null),
|
||||
'search_term' => $normalized['search'] ?? null,
|
||||
'category_id' => $normalized['category'] ?? null,
|
||||
'filters' => $normalized,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -8,12 +8,10 @@ class FavoriteServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function boot(): void
|
||||
{
|
||||
$this->loadMigrationsFrom(module_path('Favorite', 'database/migrations'));
|
||||
$this->loadMigrationsFrom(module_path('Favorite', 'Database/migrations'));
|
||||
$this->loadRoutesFrom(module_path('Favorite', 'routes/web.php'));
|
||||
$this->loadViewsFrom(module_path('Favorite', 'resources/views'), 'favorite');
|
||||
}
|
||||
|
||||
public function register(): void
|
||||
{
|
||||
}
|
||||
public function register(): void {}
|
||||
}
|
||||
|
||||
155
Modules/Favorite/Database/Seeders/FavoriteDemoSeeder.php
Normal file
155
Modules/Favorite/Database/Seeders/FavoriteDemoSeeder.php
Normal file
@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Favorite\Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Collection;
|
||||
use Modules\Category\Models\Category;
|
||||
use Modules\Favorite\App\Models\FavoriteSearch;
|
||||
use Modules\Listing\Models\Listing;
|
||||
use Modules\User\App\Models\User;
|
||||
use Modules\User\App\Support\DemoUserCatalog;
|
||||
|
||||
class FavoriteDemoSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$users = User::query()
|
||||
->whereIn('email', DemoUserCatalog::emails())
|
||||
->orderBy('email')
|
||||
->get()
|
||||
->values();
|
||||
|
||||
if ($users->count() < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
$users->each(function (User $user): void {
|
||||
$user->favoriteListings()->detach();
|
||||
$user->favoriteSellers()->detach();
|
||||
});
|
||||
|
||||
FavoriteSearch::query()->whereIn('user_id', $users->pluck('id'))->delete();
|
||||
|
||||
foreach ($users as $index => $user) {
|
||||
$favoriteSeller = $users->get(($index + 1) % $users->count());
|
||||
$secondarySeller = $users->get(($index + 2) % $users->count());
|
||||
|
||||
if (! $favoriteSeller instanceof User || ! $secondarySeller instanceof User) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$favoriteListings = Listing::query()
|
||||
->whereIn('user_id', [$favoriteSeller->getKey(), $secondarySeller->getKey()])
|
||||
->where('status', 'active')
|
||||
->orderByDesc('is_featured')
|
||||
->orderBy('id')
|
||||
->take(4)
|
||||
->get();
|
||||
|
||||
$this->seedFavoriteListings($user, $favoriteListings);
|
||||
$this->seedFavoriteSeller($user, $favoriteSeller, now()->subDays($index + 1));
|
||||
$this->seedFavoriteSearches($user, $this->searchPayloadsForUser($index));
|
||||
}
|
||||
}
|
||||
|
||||
private function seedFavoriteListings(User $user, Collection $listings): void
|
||||
{
|
||||
$payload = $listings
|
||||
->values()
|
||||
->mapWithKeys(function (Listing $listing, int $index): array {
|
||||
$timestamp = now()->subHours(8 + ($index * 3));
|
||||
|
||||
return [$listing->getKey() => [
|
||||
'created_at' => $timestamp,
|
||||
'updated_at' => $timestamp,
|
||||
]];
|
||||
})
|
||||
->all();
|
||||
|
||||
if ($payload === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
$user->favoriteListings()->syncWithoutDetaching($payload);
|
||||
}
|
||||
|
||||
private function seedFavoriteSeller(User $user, User $seller, \Illuminate\Support\Carbon $timestamp): void
|
||||
{
|
||||
if ((int) $user->getKey() === (int) $seller->getKey()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$user->favoriteSellers()->syncWithoutDetaching([
|
||||
$seller->getKey() => [
|
||||
'created_at' => $timestamp,
|
||||
'updated_at' => $timestamp,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private function seedFavoriteSearches(User $user, array $payloads): void
|
||||
{
|
||||
foreach ($payloads as $index => $payload) {
|
||||
$filters = FavoriteSearch::normalizeFilters([
|
||||
'search' => $payload['search'] ?? null,
|
||||
'category' => $payload['category_id'] ?? null,
|
||||
]);
|
||||
|
||||
if ($filters === []) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$signature = FavoriteSearch::signatureFor($filters);
|
||||
$categoryName = null;
|
||||
|
||||
if (! empty($payload['category_id'])) {
|
||||
$categoryName = Category::query()->whereKey($payload['category_id'])->value('name');
|
||||
}
|
||||
|
||||
$favoriteSearch = FavoriteSearch::updateOrCreate(
|
||||
[
|
||||
'user_id' => $user->getKey(),
|
||||
'signature' => $signature,
|
||||
],
|
||||
[
|
||||
'label' => FavoriteSearch::labelFor($filters, is_string($categoryName) ? $categoryName : null),
|
||||
'search_term' => $filters['search'] ?? null,
|
||||
'category_id' => $filters['category'] ?? null,
|
||||
'filters' => $filters,
|
||||
]
|
||||
);
|
||||
|
||||
$timestamp = now()->subDays($index + 1);
|
||||
$favoriteSearch->forceFill([
|
||||
'created_at' => $favoriteSearch->wasRecentlyCreated ? $timestamp : $favoriteSearch->created_at,
|
||||
'updated_at' => $timestamp,
|
||||
])->saveQuietly();
|
||||
}
|
||||
}
|
||||
|
||||
private function searchPayloadsForUser(int $index): array
|
||||
{
|
||||
$blueprints = [
|
||||
['search' => 'phone', 'slug' => 'electronics'],
|
||||
['search' => 'car', 'slug' => 'vehicles'],
|
||||
['search' => 'apartment', 'slug' => 'real-estate'],
|
||||
['search' => 'style', 'slug' => 'fashion'],
|
||||
['search' => 'furniture', 'slug' => 'home-garden'],
|
||||
['search' => 'fitness', 'slug' => 'sports'],
|
||||
['search' => 'remote', 'slug' => 'jobs'],
|
||||
['search' => 'cleaning', 'slug' => 'services'],
|
||||
];
|
||||
|
||||
return collect(range(0, 2))
|
||||
->map(function (int $offset) use ($blueprints, $index): array {
|
||||
$blueprint = $blueprints[($index + $offset) % count($blueprints)];
|
||||
|
||||
return [
|
||||
'search' => $blueprint['search'],
|
||||
'category_id' => Category::query()->where('slug', $blueprint['slug'])->value('id'),
|
||||
];
|
||||
})
|
||||
->all();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('favorite_listings', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||
$table->foreignId('listing_id')->constrained('listings')->cascadeOnDelete();
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['user_id', 'listing_id']);
|
||||
});
|
||||
|
||||
Schema::create('favorite_sellers', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||
$table->foreignId('seller_id')->constrained('users')->cascadeOnDelete();
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['user_id', 'seller_id']);
|
||||
});
|
||||
|
||||
Schema::create('favorite_searches', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||
$table->string('label')->nullable();
|
||||
$table->string('search_term')->nullable();
|
||||
$table->foreignId('category_id')->nullable()->constrained('categories')->nullOnDelete();
|
||||
$table->json('filters')->nullable();
|
||||
$table->string('signature', 64);
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['user_id', 'signature']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('favorite_searches');
|
||||
Schema::dropIfExists('favorite_sellers');
|
||||
Schema::dropIfExists('favorite_listings');
|
||||
}
|
||||
};
|
||||
@ -1,55 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
if (! Schema::hasTable('favorite_listings')) {
|
||||
Schema::create('favorite_listings', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||
$table->foreignId('listing_id')->constrained('listings')->cascadeOnDelete();
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['user_id', 'listing_id']);
|
||||
});
|
||||
}
|
||||
|
||||
if (! Schema::hasTable('favorite_sellers')) {
|
||||
Schema::create('favorite_sellers', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||
$table->foreignId('seller_id')->constrained('users')->cascadeOnDelete();
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['user_id', 'seller_id']);
|
||||
});
|
||||
}
|
||||
|
||||
if (! Schema::hasTable('favorite_searches')) {
|
||||
Schema::create('favorite_searches', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||
$table->string('label')->nullable();
|
||||
$table->string('search_term')->nullable();
|
||||
$table->foreignId('category_id')->nullable()->constrained('categories')->nullOnDelete();
|
||||
$table->json('filters')->nullable();
|
||||
$table->string('signature', 64);
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['user_id', 'signature']);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('favorite_searches');
|
||||
Schema::dropIfExists('favorite_sellers');
|
||||
Schema::dropIfExists('favorite_listings');
|
||||
}
|
||||
};
|
||||
@ -1,173 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Favorite\Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Modules\Category\Models\Category;
|
||||
use Modules\Favorite\App\Models\FavoriteSearch;
|
||||
use Modules\Listing\Models\Listing;
|
||||
use Modules\User\App\Models\User;
|
||||
|
||||
class FavoriteDemoSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
if (! $this->favoriteTablesExist()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$admin = User::query()->where('email', 'a@a.com')->first();
|
||||
$partner = User::query()->where('email', 'b@b.com')->first();
|
||||
|
||||
if (! $admin || ! $partner) {
|
||||
return;
|
||||
}
|
||||
|
||||
$adminListings = Listing::query()
|
||||
->where('user_id', $admin->getKey())
|
||||
->orderByDesc('is_featured')
|
||||
->orderBy('id')
|
||||
->get();
|
||||
|
||||
if ($adminListings->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$activeAdminListings = $adminListings->where('status', 'active')->values();
|
||||
|
||||
$this->seedFavoriteListings(
|
||||
$partner,
|
||||
$activeAdminListings->take(6)
|
||||
);
|
||||
|
||||
$this->seedFavoriteListings(
|
||||
$admin,
|
||||
$adminListings->take(3)->values()
|
||||
);
|
||||
|
||||
$this->seedFavoriteSeller($partner, $admin, now()->subDays(2));
|
||||
$this->seedFavoriteSeller($admin, $partner, now()->subDays(1));
|
||||
|
||||
$this->seedFavoriteSearches($partner, $this->partnerSearchPayloads());
|
||||
$this->seedFavoriteSearches($admin, $this->adminSearchPayloads());
|
||||
}
|
||||
|
||||
private function favoriteTablesExist(): bool
|
||||
{
|
||||
return Schema::hasTable('favorite_listings')
|
||||
&& Schema::hasTable('favorite_sellers')
|
||||
&& Schema::hasTable('favorite_searches');
|
||||
}
|
||||
|
||||
private function seedFavoriteListings(User $user, Collection $listings): void
|
||||
{
|
||||
$rows = $listings
|
||||
->values()
|
||||
->map(function (Listing $listing, int $index) use ($user): array {
|
||||
$timestamp = now()->subHours(12 + ($index * 5));
|
||||
|
||||
return [
|
||||
'user_id' => $user->getKey(),
|
||||
'listing_id' => $listing->getKey(),
|
||||
'created_at' => $timestamp,
|
||||
'updated_at' => $timestamp,
|
||||
];
|
||||
})
|
||||
->all();
|
||||
|
||||
if ($rows === []) {
|
||||
return;
|
||||
}
|
||||
|
||||
DB::table('favorite_listings')->upsert(
|
||||
$rows,
|
||||
['user_id', 'listing_id'],
|
||||
['updated_at']
|
||||
);
|
||||
}
|
||||
|
||||
private function seedFavoriteSeller(User $user, User $seller, \Illuminate\Support\Carbon $timestamp): void
|
||||
{
|
||||
if ((int) $user->getKey() === (int) $seller->getKey()) {
|
||||
return;
|
||||
}
|
||||
|
||||
DB::table('favorite_sellers')->upsert(
|
||||
[[
|
||||
'user_id' => $user->getKey(),
|
||||
'seller_id' => $seller->getKey(),
|
||||
'created_at' => $timestamp,
|
||||
'updated_at' => $timestamp,
|
||||
]],
|
||||
['user_id', 'seller_id'],
|
||||
['updated_at']
|
||||
);
|
||||
}
|
||||
|
||||
private function seedFavoriteSearches(User $user, array $payloads): void
|
||||
{
|
||||
foreach ($payloads as $index => $payload) {
|
||||
$filters = FavoriteSearch::normalizeFilters([
|
||||
'search' => $payload['search'] ?? null,
|
||||
'category' => $payload['category_id'] ?? null,
|
||||
]);
|
||||
|
||||
if ($filters === []) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$signature = FavoriteSearch::signatureFor($filters);
|
||||
$categoryName = null;
|
||||
|
||||
if (! empty($payload['category_id'])) {
|
||||
$categoryName = Category::query()->whereKey($payload['category_id'])->value('name');
|
||||
}
|
||||
|
||||
$favoriteSearch = FavoriteSearch::updateOrCreate(
|
||||
[
|
||||
'user_id' => $user->getKey(),
|
||||
'signature' => $signature,
|
||||
],
|
||||
[
|
||||
'label' => FavoriteSearch::labelFor($filters, is_string($categoryName) ? $categoryName : null),
|
||||
'search_term' => $filters['search'] ?? null,
|
||||
'category_id' => $filters['category'] ?? null,
|
||||
'filters' => $filters,
|
||||
]
|
||||
);
|
||||
|
||||
$timestamp = now()->subDays($index + 1);
|
||||
$favoriteSearch->forceFill([
|
||||
'created_at' => $favoriteSearch->wasRecentlyCreated ? $timestamp : $favoriteSearch->created_at,
|
||||
'updated_at' => $timestamp,
|
||||
])->saveQuietly();
|
||||
}
|
||||
}
|
||||
|
||||
private function partnerSearchPayloads(): array
|
||||
{
|
||||
$electronicsId = Category::query()->where('name', 'Electronics')->value('id');
|
||||
$vehiclesId = Category::query()->where('name', 'Vehicles')->value('id');
|
||||
$realEstateId = Category::query()->where('name', 'Real Estate')->value('id');
|
||||
|
||||
return [
|
||||
['search' => 'iphone', 'category_id' => $electronicsId],
|
||||
['search' => 'sedan', 'category_id' => $vehiclesId],
|
||||
['search' => 'apartment', 'category_id' => $realEstateId],
|
||||
];
|
||||
}
|
||||
|
||||
private function adminSearchPayloads(): array
|
||||
{
|
||||
$fashionId = Category::query()->where('name', 'Fashion')->value('id');
|
||||
$homeGardenId = Category::query()->where('name', 'Home & Garden')->value('id');
|
||||
|
||||
return [
|
||||
['search' => 'vintage', 'category_id' => $fashionId],
|
||||
['search' => 'garden', 'category_id' => $homeGardenId],
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -1,11 +1,11 @@
|
||||
@extends('app::layouts.app')
|
||||
|
||||
@section('title', 'Favoriler')
|
||||
@section('title', 'Favorites')
|
||||
|
||||
@section('content')
|
||||
<div class="max-w-[1320px] mx-auto px-4 py-8">
|
||||
<div class="grid grid-cols-1 lg:grid-cols-[220px,1fr] gap-4">
|
||||
@include('panel.partials.sidebar', ['activeMenu' => 'favorites', 'activeFavoritesTab' => $activeTab])
|
||||
@include('panel::partials.sidebar', ['activeMenu' => 'favorites', 'activeFavoritesTab' => $activeTab])
|
||||
|
||||
<section class="bg-white border border-slate-200">
|
||||
@if($requiresLogin ?? false)
|
||||
@ -29,25 +29,25 @@
|
||||
], fn ($value) => !is_null($value) && $value !== '');
|
||||
@endphp
|
||||
<div class="border-b-2 border-blue-900 px-4 py-3 flex flex-wrap items-center gap-3">
|
||||
<h1 class="text-3xl font-bold text-slate-800 mr-auto">Favori Listem</h1>
|
||||
<h1 class="text-3xl font-bold text-slate-800 mr-auto">Saved Listings</h1>
|
||||
<div class="inline-flex border border-slate-300 overflow-hidden">
|
||||
<a href="{{ route('favorites.index', array_merge($listingTabQuery, ['status' => 'all'])) }}" class="px-5 py-2 text-sm font-semibold {{ $statusFilter === 'all' ? 'bg-slate-700 text-white' : 'bg-white text-slate-700 hover:bg-slate-100' }}">
|
||||
Tümü
|
||||
All
|
||||
</a>
|
||||
<a href="{{ route('favorites.index', array_merge($listingTabQuery, ['status' => 'active'])) }}" class="px-5 py-2 text-sm font-semibold border-l border-slate-300 {{ $statusFilter === 'active' ? 'bg-slate-700 text-white' : 'bg-white text-slate-700 hover:bg-slate-100' }}">
|
||||
Yayında
|
||||
Live
|
||||
</a>
|
||||
</div>
|
||||
<form method="GET" action="{{ route('favorites.index') }}" class="flex items-center gap-2">
|
||||
<input type="hidden" name="tab" value="listings">
|
||||
<input type="hidden" name="status" value="{{ $statusFilter }}">
|
||||
<select name="category" class="h-10 min-w-44 border border-slate-300 px-3 text-sm text-slate-700">
|
||||
<option value="">Kategori</option>
|
||||
<option value="">Category</option>
|
||||
@foreach($categories as $category)
|
||||
<option value="{{ $category->id }}" @selected((int) $selectedCategoryId === (int) $category->id)>{{ $category->name }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
<button type="submit" class="h-10 px-4 bg-slate-700 text-white text-sm font-semibold hover:bg-slate-800 transition">Filtrele</button>
|
||||
<button type="submit" class="h-10 px-4 bg-slate-700 text-white text-sm font-semibold hover:bg-slate-800 transition">Filter</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@ -55,17 +55,17 @@
|
||||
<table class="w-full min-w-[860px]">
|
||||
<thead>
|
||||
<tr class="bg-slate-50 text-slate-700 text-sm">
|
||||
<th class="text-left px-4 py-3 w-[58%]">İlan Başlığı</th>
|
||||
<th class="text-left px-4 py-3 w-[16%]">Fiyat</th>
|
||||
<th class="text-left px-4 py-3 w-[14%]">Mesajlaşma</th>
|
||||
<th class="text-left px-4 py-3 w-[58%]">Listing</th>
|
||||
<th class="text-left px-4 py-3 w-[16%]">Price</th>
|
||||
<th class="text-left px-4 py-3 w-[14%]">Messaging</th>
|
||||
<th class="text-right px-4 py-3 w-[12%]"></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@forelse($favoriteListings as $listing)
|
||||
@php
|
||||
$listingImage = $listing->getFirstMediaUrl('listing-images');
|
||||
$priceLabel = $listing->price ? number_format((float) $listing->price, 0).' '.$listing->currency : 'Ücretsiz';
|
||||
$listingImage = $listing->primaryImageData('card');
|
||||
$priceLabel = $listing->price ? number_format((float) $listing->price, 0).' '.$listing->currency : 'Free';
|
||||
$meta = collect([
|
||||
$listing->category?->name,
|
||||
$listing->city,
|
||||
@ -80,17 +80,21 @@
|
||||
<div class="flex gap-3">
|
||||
<a href="{{ route('listings.show', $listing) }}" class="w-36 h-24 shrink-0 bg-slate-100 border border-slate-200 overflow-hidden">
|
||||
@if($listingImage)
|
||||
<img src="{{ $listingImage }}" alt="{{ $listing->title }}" class="w-full h-full object-cover">
|
||||
@include('listing::partials.responsive-image', [
|
||||
'image' => $listingImage,
|
||||
'alt' => $listing->title,
|
||||
'class' => 'w-full h-full object-cover',
|
||||
])
|
||||
@else
|
||||
<div class="w-full h-full grid place-items-center text-slate-400">Görsel yok</div>
|
||||
<div class="w-full h-full grid place-items-center text-slate-400">No image</div>
|
||||
@endif
|
||||
</a>
|
||||
<div>
|
||||
<a href="{{ route('listings.show', $listing) }}" class="font-semibold text-2xl text-slate-800 hover:text-blue-700 transition leading-6">
|
||||
{{ $listing->title }}
|
||||
</a>
|
||||
<p class="text-sm text-slate-500 mt-2">{{ $meta !== '' ? $meta : 'Kategori / konum bilgisi yok' }}</p>
|
||||
<p class="text-xs text-slate-400 mt-1">Favoriye eklenme: {{ $listing->pivot->created_at?->format('d.m.Y') }}</p>
|
||||
<p class="text-sm text-slate-500 mt-2">{{ $meta !== '' ? $meta : 'No category or location data' }}</p>
|
||||
<p class="text-xs text-slate-400 mt-1">Saved on: {{ $listing->pivot->created_at?->format('M j, Y') }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
@ -99,31 +103,31 @@
|
||||
@if($canMessageListing)
|
||||
@if($conversationId)
|
||||
<a href="{{ route('panel.inbox.index', ['conversation' => $conversationId]) }}" class="inline-flex items-center h-10 px-4 border border-rose-300 text-rose-600 text-sm font-semibold rounded-full hover:bg-rose-50 transition">
|
||||
Sohbete Git
|
||||
Open chat
|
||||
</a>
|
||||
@else
|
||||
<form method="POST" action="{{ route('conversations.start', $listing) }}">
|
||||
@csrf
|
||||
<button type="submit" class="inline-flex items-center h-10 px-4 bg-rose-500 text-white text-sm font-semibold rounded-full hover:bg-rose-600 transition">
|
||||
Mesaj Gönder
|
||||
Send message
|
||||
</button>
|
||||
</form>
|
||||
@endif
|
||||
@else
|
||||
<span class="text-xs text-slate-400">{{ $isOwnListing ? 'Kendi ilanın' : 'Satıcı bilgisi yok' }}</span>
|
||||
<span class="text-xs text-slate-400">{{ $isOwnListing ? 'Your own listing' : 'Seller unavailable' }}</span>
|
||||
@endif
|
||||
</td>
|
||||
<td class="px-4 py-4 text-right">
|
||||
<form method="POST" action="{{ route('favorites.listings.toggle', $listing) }}">
|
||||
@csrf
|
||||
<button type="submit" class="text-sm font-semibold text-rose-500 hover:text-rose-600 transition">Kaldır</button>
|
||||
<button type="submit" class="text-sm font-semibold text-rose-500 hover:text-rose-600 transition">Remove</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
@empty
|
||||
<tr class="border-t border-slate-200">
|
||||
<td colspan="4" class="px-4 py-10 text-center text-slate-500">
|
||||
Henüz favori ilan bulunmuyor.
|
||||
No saved listings yet.
|
||||
</td>
|
||||
</tr>
|
||||
@endforelse
|
||||
@ -132,7 +136,7 @@
|
||||
</div>
|
||||
|
||||
<div class="px-4 py-4 border-t border-slate-200 text-sm text-slate-500">
|
||||
* Son 1 yıl içinde favoriye eklediğiniz ilanlar listelenmektedir.
|
||||
* Listings saved within the last year are shown here.
|
||||
</div>
|
||||
|
||||
@if($favoriteListings?->hasPages())
|
||||
@ -142,8 +146,8 @@
|
||||
|
||||
@if($activeTab === 'searches')
|
||||
<div class="px-4 py-4 border-b border-slate-200">
|
||||
<h1 class="text-3xl font-bold text-slate-800">Favori Aramalar</h1>
|
||||
<p class="text-sm text-slate-500 mt-1">Kayıtlı aramalarına tek tıkla geri dön.</p>
|
||||
<h1 class="text-3xl font-bold text-slate-800">Saved Searches</h1>
|
||||
<p class="text-sm text-slate-500 mt-1">Return to your saved searches with one click.</p>
|
||||
</div>
|
||||
<div class="divide-y divide-slate-200">
|
||||
@forelse($favoriteSearches as $favoriteSearch)
|
||||
@ -155,29 +159,29 @@
|
||||
@endphp
|
||||
<article class="px-4 py-4 flex flex-col md:flex-row md:items-center gap-3">
|
||||
<div class="flex-1">
|
||||
<h2 class="font-semibold text-slate-800">{{ $favoriteSearch->label ?: 'Kayıtlı arama' }}</h2>
|
||||
<h2 class="font-semibold text-slate-800">{{ $favoriteSearch->label ?: 'Saved search' }}</h2>
|
||||
<p class="text-sm text-slate-500 mt-1">
|
||||
@if($favoriteSearch->search_term) Arama: "{{ $favoriteSearch->search_term }}" · @endif
|
||||
@if($favoriteSearch->category) Kategori: {{ $favoriteSearch->category->name }} · @endif
|
||||
Kaydedilme: {{ $favoriteSearch->created_at?->format('d.m.Y H:i') }}
|
||||
@if($favoriteSearch->search_term) Search: "{{ $favoriteSearch->search_term }}" · @endif
|
||||
@if($favoriteSearch->category) Category: {{ $favoriteSearch->category->name }} · @endif
|
||||
Saved: {{ $favoriteSearch->created_at?->format('M j, Y H:i') }}
|
||||
</p>
|
||||
</div>
|
||||
<div class="flex items-center gap-3">
|
||||
<a href="{{ $searchUrl }}" class="inline-flex items-center h-10 px-4 bg-blue-600 text-white text-sm font-semibold rounded hover:bg-blue-700 transition">
|
||||
Aramayı Aç
|
||||
Open search
|
||||
</a>
|
||||
<form method="POST" action="{{ route('favorites.searches.destroy', $favoriteSearch) }}">
|
||||
@csrf
|
||||
@method('DELETE')
|
||||
<button type="submit" class="inline-flex items-center h-10 px-4 border border-slate-300 text-sm font-semibold text-slate-700 hover:bg-slate-50 transition">
|
||||
Sil
|
||||
Delete
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
</article>
|
||||
@empty
|
||||
<div class="px-4 py-10 text-center text-slate-500">
|
||||
Henüz favori arama eklenmedi.
|
||||
No saved searches yet.
|
||||
</div>
|
||||
@endforelse
|
||||
</div>
|
||||
@ -188,32 +192,32 @@
|
||||
|
||||
@if($activeTab === 'sellers')
|
||||
<div class="px-4 py-4 border-b border-slate-200">
|
||||
<h1 class="text-3xl font-bold text-slate-800">Favori Satıcılar</h1>
|
||||
<p class="text-sm text-slate-500 mt-1">Takip etmek istediğin satıcıları burada yönetebilirsin.</p>
|
||||
<h1 class="text-3xl font-bold text-slate-800">Saved Sellers</h1>
|
||||
<p class="text-sm text-slate-500 mt-1">Manage the sellers you want to follow here.</p>
|
||||
</div>
|
||||
<div class="divide-y divide-slate-200">
|
||||
@forelse($favoriteSellers as $seller)
|
||||
<article class="px-4 py-4 flex flex-col md:flex-row md:items-center gap-3">
|
||||
<div class="flex items-center gap-3 flex-1">
|
||||
<a href="{{ route('listings.index', ['user' => $seller->id]) }}" class="flex items-center gap-3 flex-1 hover:opacity-90 transition">
|
||||
<div class="w-12 h-12 rounded-full bg-blue-100 text-blue-700 font-bold grid place-items-center">
|
||||
{{ strtoupper(substr((string) $seller->name, 0, 1)) }}
|
||||
</div>
|
||||
<div>
|
||||
<h2 class="font-semibold text-slate-800">{{ $seller->name }}</h2>
|
||||
<p class="text-sm text-slate-500">{{ $seller->email }}</p>
|
||||
<p class="text-xs text-slate-400 mt-1">Aktif ilan: {{ (int) $seller->active_listings_count }}</p>
|
||||
<p class="text-xs text-slate-400 mt-1">Active listings: {{ (int) $seller->active_listings_count }}</p>
|
||||
</div>
|
||||
</div>
|
||||
</a>
|
||||
<form method="POST" action="{{ route('favorites.sellers.toggle', $seller) }}">
|
||||
@csrf
|
||||
<button type="submit" class="inline-flex items-center h-10 px-4 border border-rose-200 text-sm font-semibold text-rose-600 hover:bg-rose-50 transition">
|
||||
Favoriden Kaldır
|
||||
Remove seller
|
||||
</button>
|
||||
</form>
|
||||
</article>
|
||||
@empty
|
||||
<div class="px-4 py-10 text-center text-slate-500">
|
||||
Henüz favori satıcı eklenmedi.
|
||||
No saved sellers yet.
|
||||
</div>
|
||||
@endforelse
|
||||
</div>
|
||||
|
||||
@ -3,13 +3,15 @@
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use Modules\Favorite\App\Http\Controllers\FavoriteController;
|
||||
|
||||
Route::prefix('favorites')->name('favorites.')->group(function () {
|
||||
Route::get('/', [FavoriteController::class, 'index'])->name('index');
|
||||
});
|
||||
Route::middleware('web')->group(function () {
|
||||
Route::prefix('favorites')->name('favorites.')->group(function () {
|
||||
Route::get('/', [FavoriteController::class, 'index'])->name('index');
|
||||
});
|
||||
|
||||
Route::middleware('auth')->prefix('favorites')->name('favorites.')->group(function () {
|
||||
Route::post('/listings/{listing}/toggle', [FavoriteController::class, 'toggleListing'])->name('listings.toggle');
|
||||
Route::post('/sellers/{seller}/toggle', [FavoriteController::class, 'toggleSeller'])->name('sellers.toggle');
|
||||
Route::post('/searches', [FavoriteController::class, 'storeSearch'])->name('searches.store');
|
||||
Route::delete('/searches/{favoriteSearch}', [FavoriteController::class, 'destroySearch'])->name('searches.destroy');
|
||||
Route::middleware('auth')->prefix('favorites')->name('favorites.')->group(function () {
|
||||
Route::post('/listings/{listing}/toggle', [FavoriteController::class, 'toggleListing'])->name('listings.toggle');
|
||||
Route::post('/sellers/{seller}/toggle', [FavoriteController::class, 'toggleSeller'])->name('sellers.toggle');
|
||||
Route::post('/searches', [FavoriteController::class, 'storeSearch'])->name('searches.store');
|
||||
Route::delete('/searches/{favoriteSearch}', [FavoriteController::class, 'destroySearch'])->name('searches.destroy');
|
||||
});
|
||||
});
|
||||
|
||||
@ -3,173 +3,79 @@
|
||||
namespace Modules\Listing\Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Category\Models\Category;
|
||||
use Modules\Listing\Models\Listing;
|
||||
use Modules\User\App\Models\User;
|
||||
use Modules\User\App\Support\DemoUserCatalog;
|
||||
|
||||
class ListingPanelDemoSeeder extends Seeder
|
||||
{
|
||||
private const PANEL_LISTINGS = [
|
||||
[
|
||||
'slug' => 'admin-demo-sold-camera',
|
||||
'title' => 'Admin Demo Camera Bundle',
|
||||
'description' => 'Sample sold listing for the panel filters and activity cards.',
|
||||
'price' => 18450,
|
||||
'status' => 'sold',
|
||||
'city' => 'Istanbul',
|
||||
'country' => 'Turkey',
|
||||
'image' => 'sample_image/macbook.jpg',
|
||||
'expires_offset_days' => 12,
|
||||
'is_featured' => false,
|
||||
],
|
||||
[
|
||||
'slug' => 'admin-demo-expired-sofa',
|
||||
'title' => 'Admin Demo Sofa Set',
|
||||
'description' => 'Sample expired listing for the panel filters and republish flow.',
|
||||
'price' => 9800,
|
||||
'status' => 'expired',
|
||||
'city' => 'Ankara',
|
||||
'country' => 'Turkey',
|
||||
'image' => 'sample_image/cup.jpg',
|
||||
'expires_offset_days' => -5,
|
||||
'is_featured' => false,
|
||||
],
|
||||
[
|
||||
'slug' => 'admin-demo-expired-bike',
|
||||
'title' => 'Admin Demo City Bike',
|
||||
'description' => 'Extra expired sample listing so My Listings is not empty in filtered views.',
|
||||
'price' => 6200,
|
||||
'status' => 'expired',
|
||||
'city' => 'Izmir',
|
||||
'country' => 'Turkey',
|
||||
'image' => 'sample_image/car2.jpeg',
|
||||
'expires_offset_days' => -11,
|
||||
'is_featured' => false,
|
||||
],
|
||||
private const LEGACY_SLUGS = [
|
||||
'admin-demo-pro-workstation',
|
||||
'admin-demo-sold-camera',
|
||||
'admin-demo-expired-sofa',
|
||||
'member-demo-iphone',
|
||||
'member-demo-city-bike',
|
||||
'member-demo-vintage-chair',
|
||||
'member-demo-garden-tools',
|
||||
];
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$admin = $this->resolveAdminUser();
|
||||
Listing::query()
|
||||
->whereIn('slug', self::LEGACY_SLUGS)
|
||||
->get()
|
||||
->each(function (Listing $listing): void {
|
||||
$listing->clearMediaCollection('listing-images');
|
||||
$listing->delete();
|
||||
});
|
||||
|
||||
if (! $admin) {
|
||||
return;
|
||||
}
|
||||
foreach (DemoUserCatalog::emails() as $email) {
|
||||
$user = User::query()->where('email', $email)->first();
|
||||
|
||||
$this->claimAllListingsForAdmin($admin);
|
||||
|
||||
$categories = $this->resolveCategories();
|
||||
|
||||
if ($categories->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (self::PANEL_LISTINGS as $index => $payload) {
|
||||
$category = $categories->get($index % $categories->count());
|
||||
|
||||
if (! $category instanceof Category) {
|
||||
if (! $user) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$listing = Listing::updateOrCreate(
|
||||
['slug' => $payload['slug']],
|
||||
[
|
||||
'slug' => $payload['slug'],
|
||||
'title' => $payload['title'],
|
||||
'description' => $payload['description'],
|
||||
'price' => $payload['price'],
|
||||
'currency' => 'TRY',
|
||||
'city' => $payload['city'],
|
||||
'country' => $payload['country'],
|
||||
'category_id' => $category->getKey(),
|
||||
'user_id' => $admin->getKey(),
|
||||
'status' => $payload['status'],
|
||||
'contact_email' => $admin->email,
|
||||
'contact_phone' => '+905551112233',
|
||||
'is_featured' => $payload['is_featured'],
|
||||
'expires_at' => now()->addDays((int) $payload['expires_offset_days']),
|
||||
]
|
||||
);
|
||||
|
||||
$this->syncListingImage($listing, (string) $payload['image']);
|
||||
$this->applyPanelStates($user);
|
||||
}
|
||||
}
|
||||
|
||||
private function resolveAdminUser(): ?User
|
||||
private function applyPanelStates(User $user): void
|
||||
{
|
||||
return User::query()->where('email', 'a@a.com')->first()
|
||||
?? User::query()->whereHas('roles', fn ($query) => $query->where('name', 'admin'))->first()
|
||||
?? User::query()->first();
|
||||
}
|
||||
|
||||
private function claimAllListingsForAdmin(User $admin): void
|
||||
{
|
||||
Listing::query()
|
||||
->where(function ($query) use ($admin): void {
|
||||
$query
|
||||
->whereNull('user_id')
|
||||
->orWhere('user_id', '!=', $admin->getKey());
|
||||
})
|
||||
->update([
|
||||
'user_id' => $admin->getKey(),
|
||||
'contact_email' => $admin->email,
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
|
||||
private function resolveCategories(): Collection
|
||||
{
|
||||
$leafCategories = Category::query()
|
||||
->where('is_active', true)
|
||||
->whereDoesntHave('children')
|
||||
->orderBy('sort_order')
|
||||
->orderBy('name')
|
||||
->get();
|
||||
|
||||
if ($leafCategories->isNotEmpty()) {
|
||||
return $leafCategories->values();
|
||||
}
|
||||
|
||||
return Category::query()
|
||||
->where('is_active', true)
|
||||
->orderBy('sort_order')
|
||||
->orderBy('name')
|
||||
$listings = Listing::query()
|
||||
->where('user_id', $user->getKey())
|
||||
->where('slug', 'like', 'demo-%')
|
||||
->orderBy('created_at')
|
||||
->get()
|
||||
->values();
|
||||
|
||||
foreach ($listings as $index => $listing) {
|
||||
$listing->forceFill([
|
||||
'status' => $this->statusForIndex($index),
|
||||
'is_featured' => $index < 2,
|
||||
'expires_at' => $this->expiresAtForIndex($index),
|
||||
'updated_at' => now()->subHours($index),
|
||||
])->saveQuietly();
|
||||
}
|
||||
}
|
||||
|
||||
private function syncListingImage(Listing $listing, string $imageRelativePath): void
|
||||
private function statusForIndex(int $index): string
|
||||
{
|
||||
$imageAbsolutePath = public_path($imageRelativePath);
|
||||
return match ($index % 9) {
|
||||
2 => 'sold',
|
||||
3 => 'expired',
|
||||
4 => 'pending',
|
||||
default => 'active',
|
||||
};
|
||||
}
|
||||
|
||||
if (! is_file($imageAbsolutePath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$targetFileName = basename($imageAbsolutePath);
|
||||
$existingMedia = $listing->getMedia('listing-images')->first();
|
||||
|
||||
if (
|
||||
$existingMedia
|
||||
&& (string) $existingMedia->file_name === $targetFileName
|
||||
&& (string) $existingMedia->disk === 'public'
|
||||
) {
|
||||
try {
|
||||
if (is_file($existingMedia->getPath())) {
|
||||
return;
|
||||
}
|
||||
} catch (\Throwable) {
|
||||
}
|
||||
}
|
||||
|
||||
$listing->clearMediaCollection('listing-images');
|
||||
|
||||
$listing
|
||||
->addMedia($imageAbsolutePath)
|
||||
->usingFileName(Str::slug($listing->slug).'-'.basename($imageAbsolutePath))
|
||||
->preservingOriginal()
|
||||
->toMediaCollection('listing-images', 'public');
|
||||
private function expiresAtForIndex(int $index): \Illuminate\Support\Carbon
|
||||
{
|
||||
return match ($this->statusForIndex($index)) {
|
||||
'expired' => now()->subDays(4 + ($index % 5)),
|
||||
'sold' => now()->addDays(8 + ($index % 4)),
|
||||
'pending' => now()->addDays(5 + ($index % 4)),
|
||||
default => now()->addDays(20 + ($index % 9)),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,63 +4,81 @@ namespace Modules\Listing\Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Category\Models\Category;
|
||||
use Modules\Listing\Models\Listing;
|
||||
use Modules\Listing\Support\SampleListingImageCatalog;
|
||||
use Modules\Location\Models\City;
|
||||
use Modules\Location\Models\Country;
|
||||
use Modules\User\App\Models\User;
|
||||
use Modules\User\App\Support\DemoUserCatalog;
|
||||
|
||||
class ListingSeeder extends Seeder
|
||||
{
|
||||
private const SAMPLE_IMAGES = [
|
||||
'sample_image/phone.jpeg',
|
||||
'sample_image/macbook.jpg',
|
||||
'sample_image/car.jpeg',
|
||||
'sample_image/headphones.jpg',
|
||||
'sample_image/laptop.jpg',
|
||||
'sample_image/cup.jpg',
|
||||
'sample_image/car2.jpeg',
|
||||
];
|
||||
|
||||
private const TITLE_PREFIXES = [
|
||||
'Temiz kullanılmış',
|
||||
'Az kullanılmış',
|
||||
'Fırsat ürün',
|
||||
'Uygun fiyatlı',
|
||||
'Sahibinden',
|
||||
'Kaçırılmayacak',
|
||||
'Bakımlı',
|
||||
'Clean',
|
||||
'Lightly used',
|
||||
'Special offer',
|
||||
'Well priced',
|
||||
'Owner listed',
|
||||
'Must-see',
|
||||
'Well kept',
|
||||
];
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$user = $this->resolveSeederUser();
|
||||
$users = $this->resolveSeederUsers();
|
||||
$categories = $this->resolveSeedableCategories();
|
||||
$imagePool = SampleListingImageCatalog::uniquePaths();
|
||||
|
||||
if (! $user || $categories->isEmpty()) {
|
||||
if ($users->isEmpty() || $categories->isEmpty() || $imagePool->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$countries = $this->resolveCountries();
|
||||
$turkeyCities = $this->resolveTurkeyCities();
|
||||
$plannedSlugs = [];
|
||||
$assignedImageIndex = 0;
|
||||
|
||||
foreach ($categories as $index => $category) {
|
||||
$listingData = $this->buildListingData($category, $index, $countries, $turkeyCities);
|
||||
$listing = $this->upsertListing($index, $listingData, $category, $user);
|
||||
$this->syncListingImage($listing, $listingData['image']);
|
||||
foreach ($categories as $category) {
|
||||
foreach ($users as $user) {
|
||||
if ($assignedImageIndex >= $imagePool->count()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$listingData = $this->buildListingData(
|
||||
$category,
|
||||
$assignedImageIndex,
|
||||
$countries,
|
||||
$turkeyCities,
|
||||
$user,
|
||||
$imagePool->get($assignedImageIndex)
|
||||
);
|
||||
$listing = $this->upsertListing($listingData, $category, $user);
|
||||
$plannedSlugs[] = $listing->slug;
|
||||
$this->syncListingImage($listing, $listingData['image_path']);
|
||||
$assignedImageIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
Listing::query()
|
||||
->whereIn('user_id', $users->pluck('id'))
|
||||
->where('slug', 'like', 'demo-%')
|
||||
->whereNotIn('slug', $plannedSlugs)
|
||||
->get()
|
||||
->each(function (Listing $listing): void {
|
||||
$listing->clearMediaCollection('listing-images');
|
||||
$listing->delete();
|
||||
});
|
||||
}
|
||||
|
||||
private function resolveSeederUser(): ?User
|
||||
private function resolveSeederUsers(): Collection
|
||||
{
|
||||
return User::query()->where('email', 'a@a.com')->first()
|
||||
?? User::query()->where('email', 'admin@openclassify.com')->first()
|
||||
?? User::query()
|
||||
->whereHas('roles', fn ($query) => $query->where('name', 'admin'))
|
||||
->first()
|
||||
?? User::query()->first();
|
||||
return User::query()
|
||||
->whereIn('email', DemoUserCatalog::emails())
|
||||
->orderBy('email')
|
||||
->get()
|
||||
->values();
|
||||
}
|
||||
|
||||
private function resolveSeedableCategories(): Collection
|
||||
@ -68,6 +86,7 @@ class ListingSeeder extends Seeder
|
||||
$leafCategories = Category::query()
|
||||
->where('is_active', true)
|
||||
->whereDoesntHave('children')
|
||||
->with('parent:id,name')
|
||||
->orderBy('sort_order')
|
||||
->orderBy('name')
|
||||
->get();
|
||||
@ -78,6 +97,7 @@ class ListingSeeder extends Seeder
|
||||
|
||||
return Category::query()
|
||||
->where('is_active', true)
|
||||
->with('parent:id,name')
|
||||
->orderBy('sort_order')
|
||||
->orderBy('name')
|
||||
->get()
|
||||
@ -86,10 +106,6 @@ class ListingSeeder extends Seeder
|
||||
|
||||
private function resolveCountries(): Collection
|
||||
{
|
||||
if (! class_exists(Country::class) || ! Schema::hasTable('countries')) {
|
||||
return collect();
|
||||
}
|
||||
|
||||
return Country::query()
|
||||
->where('is_active', true)
|
||||
->orderBy('name')
|
||||
@ -99,16 +115,12 @@ class ListingSeeder extends Seeder
|
||||
|
||||
private function resolveTurkeyCities(): Collection
|
||||
{
|
||||
if (! class_exists(City::class) || ! Schema::hasTable('cities') || ! Schema::hasTable('countries')) {
|
||||
return collect(['İstanbul', 'Ankara', 'İzmir', 'Bursa', 'Antalya']);
|
||||
}
|
||||
|
||||
$turkey = Country::query()
|
||||
->where('code', 'TR')
|
||||
->first(['id']);
|
||||
|
||||
if (! $turkey) {
|
||||
return collect(['İstanbul', 'Ankara', 'İzmir', 'Bursa', 'Antalya']);
|
||||
return collect(['Istanbul', 'Ankara', 'Izmir', 'Bursa', 'Antalya']);
|
||||
}
|
||||
|
||||
$cities = City::query()
|
||||
@ -122,33 +134,40 @@ class ListingSeeder extends Seeder
|
||||
|
||||
return $cities->isNotEmpty()
|
||||
? $cities
|
||||
: collect(['İstanbul', 'Ankara', 'İzmir', 'Bursa', 'Antalya']);
|
||||
: collect(['Istanbul', 'Ankara', 'Izmir', 'Bursa', 'Antalya']);
|
||||
}
|
||||
|
||||
private function buildListingData(
|
||||
Category $category,
|
||||
int $index,
|
||||
Collection $countries,
|
||||
Collection $turkeyCities
|
||||
Collection $turkeyCities,
|
||||
User $user,
|
||||
?string $imagePath
|
||||
): array {
|
||||
$location = $this->resolveLocation($index, $countries, $turkeyCities);
|
||||
$image = self::SAMPLE_IMAGES[$index % count(self::SAMPLE_IMAGES)];
|
||||
$title = $this->buildTitle($category, $index, $user);
|
||||
$slug = 'demo-'.Str::slug($user->email).'-'.$category->slug;
|
||||
|
||||
return [
|
||||
'title' => $this->buildTitle($category, $index),
|
||||
'description' => $this->buildDescription($category, $location['city'], $location['country']),
|
||||
'slug' => $slug,
|
||||
'title' => $title,
|
||||
'description' => $this->buildDescription($category, $location['city'], $location['country'], $user),
|
||||
'price' => $this->priceForIndex($index),
|
||||
'city' => $location['city'],
|
||||
'country' => $location['country'],
|
||||
'image' => $image,
|
||||
'contact_phone' => DemoUserCatalog::phoneFor($user->email),
|
||||
'is_featured' => $index % 7 === 0,
|
||||
'expires_at' => now()->addDays(21 + ($index % 9)),
|
||||
'created_at' => now()->subHours(6 + $index),
|
||||
'image_path' => $imagePath,
|
||||
];
|
||||
}
|
||||
|
||||
private function resolveLocation(int $index, Collection $countries, Collection $turkeyCities): array
|
||||
{
|
||||
$turkeyCountry = $countries->first(fn ($country): bool => strtoupper((string) $country->code) === 'TR');
|
||||
$turkeyName = trim((string) ($turkeyCountry->name ?? 'Türkiye')) ?: 'Türkiye';
|
||||
|
||||
$turkeyName = trim((string) ($turkeyCountry->name ?? 'Turkey')) ?: 'Turkey';
|
||||
$useForeignCountry = $countries->count() > 1 && $index % 4 === 0;
|
||||
|
||||
if ($useForeignCountry) {
|
||||
@ -161,8 +180,8 @@ class ListingSeeder extends Seeder
|
||||
$countryName = trim((string) ($selected->name ?? ''));
|
||||
|
||||
return [
|
||||
'country' => $countryName !== '' ? $countryName : 'Türkiye',
|
||||
'city' => $countryName !== '' ? $countryName : 'İstanbul',
|
||||
'country' => $countryName !== '' ? $countryName : 'Turkey',
|
||||
'city' => $countryName !== '' ? $countryName : 'Istanbul',
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -171,27 +190,34 @@ class ListingSeeder extends Seeder
|
||||
|
||||
return [
|
||||
'country' => $turkeyName,
|
||||
'city' => $city !== '' ? $city : 'İstanbul',
|
||||
'city' => $city !== '' ? $city : 'Istanbul',
|
||||
];
|
||||
}
|
||||
|
||||
private function buildTitle(Category $category, int $index): string
|
||||
private function buildTitle(Category $category, int $index, User $user): string
|
||||
{
|
||||
$prefix = self::TITLE_PREFIXES[$index % count(self::TITLE_PREFIXES)];
|
||||
$categoryName = trim((string) $category->name);
|
||||
$ownerFragment = trim(Str::before($user->name, ' '));
|
||||
|
||||
return sprintf('%s %s ilanı', $prefix, $categoryName !== '' ? $categoryName : 'ürün');
|
||||
return sprintf(
|
||||
'%s %s for %s',
|
||||
$prefix,
|
||||
$categoryName !== '' ? $categoryName : 'item',
|
||||
$ownerFragment !== '' ? $ownerFragment : 'demo'
|
||||
);
|
||||
}
|
||||
|
||||
private function buildDescription(Category $category, string $city, string $country): string
|
||||
private function buildDescription(Category $category, string $city, string $country, User $user): string
|
||||
{
|
||||
$categoryName = trim((string) $category->name);
|
||||
$location = trim(collect([$city, $country])->filter()->join(', '));
|
||||
|
||||
return sprintf(
|
||||
'%s kategorisinde, durum olarak sorunsuz ve kullanıma hazırdır. Teslimat noktası: %s. Detaylar için mesaj atabilirsiniz.',
|
||||
$categoryName !== '' ? $categoryName : 'Ürün',
|
||||
$location !== '' ? $location : 'Türkiye'
|
||||
'%s listed by %s. Clean demo condition, sample product photo assigned from the provided catalog, and ready for browsing, favorites, inbox, and panel testing. Pickup area: %s.',
|
||||
$categoryName !== '' ? $categoryName : 'Item',
|
||||
trim((string) $user->name) !== '' ? trim((string) $user->name) : 'a marketplace user',
|
||||
$location !== '' ? $location : 'Turkey'
|
||||
);
|
||||
}
|
||||
|
||||
@ -214,14 +240,12 @@ class ListingSeeder extends Seeder
|
||||
return $base + $step;
|
||||
}
|
||||
|
||||
private function upsertListing(int $index, array $data, Category $category, User $user): Listing
|
||||
private function upsertListing(array $data, Category $category, User $user): Listing
|
||||
{
|
||||
$slug = Str::slug($category->slug.'-'.$data['title']).'-'.($index + 1);
|
||||
|
||||
return Listing::updateOrCreate(
|
||||
['slug' => $slug],
|
||||
$listing = Listing::updateOrCreate(
|
||||
['slug' => $data['slug']],
|
||||
[
|
||||
'slug' => $slug,
|
||||
'slug' => $data['slug'],
|
||||
'title' => $data['title'],
|
||||
'description' => $data['description'],
|
||||
'price' => $data['price'],
|
||||
@ -232,57 +256,29 @@ class ListingSeeder extends Seeder
|
||||
'user_id' => $user->id,
|
||||
'status' => 'active',
|
||||
'contact_email' => $user->email,
|
||||
'contact_phone' => '+905551112233',
|
||||
'is_featured' => $index < 8,
|
||||
'contact_phone' => $data['contact_phone'],
|
||||
'is_featured' => $data['is_featured'],
|
||||
'expires_at' => $data['expires_at'],
|
||||
]
|
||||
);
|
||||
|
||||
$listing->forceFill([
|
||||
'created_at' => $data['created_at'],
|
||||
'updated_at' => $data['created_at'],
|
||||
])->saveQuietly();
|
||||
|
||||
return $listing;
|
||||
}
|
||||
|
||||
private function syncListingImage(Listing $listing, string $imageRelativePath): void
|
||||
private function syncListingImage(Listing $listing, ?string $imageAbsolutePath): void
|
||||
{
|
||||
$imageAbsolutePath = public_path($imageRelativePath);
|
||||
|
||||
if (! is_file($imageAbsolutePath)) {
|
||||
if ($this->command) {
|
||||
$this->command->warn("Gorsel bulunamadi: {$imageRelativePath}");
|
||||
}
|
||||
|
||||
if (! is_string($imageAbsolutePath) || ! is_file($imageAbsolutePath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$targetFileName = basename($imageAbsolutePath);
|
||||
$mediaItems = $listing->getMedia('listing-images');
|
||||
|
||||
if (! $this->hasSingleHealthyTargetMedia($mediaItems, $targetFileName)) {
|
||||
$listing->clearMediaCollection('listing-images');
|
||||
|
||||
$listing
|
||||
->addMedia($imageAbsolutePath)
|
||||
->preservingOriginal()
|
||||
->toMediaCollection('listing-images', 'public');
|
||||
}
|
||||
}
|
||||
|
||||
private function hasSingleHealthyTargetMedia(Collection $mediaItems, string $targetFileName): bool
|
||||
{
|
||||
if ($mediaItems->count() !== 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$media = $mediaItems->first();
|
||||
|
||||
if (
|
||||
! $media
|
||||
|| (string) $media->file_name !== $targetFileName
|
||||
|| (string) $media->disk !== 'public'
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return is_file($media->getPath());
|
||||
} catch (\Throwable) {
|
||||
return false;
|
||||
}
|
||||
$listing->replacePublicImage(
|
||||
$imageAbsolutePath,
|
||||
SampleListingImageCatalog::fileNameFor($imageAbsolutePath, $listing->slug)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
@ -30,10 +31,26 @@ return new class extends Migration
|
||||
$table->decimal('longitude', 10, 7)->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
Schema::create('listing_custom_fields', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->string('name')->unique();
|
||||
$table->string('label');
|
||||
$table->string('type', 32);
|
||||
$table->foreignId('category_id')->nullable()->constrained('categories')->nullOnDelete();
|
||||
$table->text('placeholder')->nullable();
|
||||
$table->text('help_text')->nullable();
|
||||
$table->json('options')->nullable();
|
||||
$table->boolean('is_required')->default(false);
|
||||
$table->boolean('is_active')->default(true);
|
||||
$table->unsignedInteger('sort_order')->default(0);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('listing_custom_fields');
|
||||
Schema::dropIfExists('listings');
|
||||
}
|
||||
};
|
||||
@ -1,31 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('listing_custom_fields', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->string('name')->unique();
|
||||
$table->string('label');
|
||||
$table->string('type', 32);
|
||||
$table->foreignId('category_id')->nullable()->constrained('categories')->nullOnDelete();
|
||||
$table->text('placeholder')->nullable();
|
||||
$table->text('help_text')->nullable();
|
||||
$table->json('options')->nullable();
|
||||
$table->boolean('is_required')->default(false);
|
||||
$table->boolean('is_active')->default(true);
|
||||
$table->unsignedInteger('sort_order')->default(0);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('listing_custom_fields');
|
||||
}
|
||||
};
|
||||
@ -29,4 +29,9 @@ return new class extends Migration
|
||||
$table->nullableTimestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('media');
|
||||
}
|
||||
};
|
||||
@ -1,30 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Admin\Filament\Resources;
|
||||
namespace Modules\Listing\Filament\Admin\Resources;
|
||||
|
||||
use BackedEnum;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Actions\EditAction;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TagsInput;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
use Modules\Admin\Filament\Resources\ListingCustomFieldResource\Pages;
|
||||
use Modules\Admin\Support\Filament\ResourceTableActions;
|
||||
use Modules\Category\Models\Category;
|
||||
use Modules\Listing\Filament\Admin\Resources\ListingCustomFieldResource\Pages;
|
||||
use Modules\Listing\Models\ListingCustomField;
|
||||
use UnitEnum;
|
||||
|
||||
class ListingCustomFieldResource extends Resource
|
||||
{
|
||||
protected static ?string $model = ListingCustomField::class;
|
||||
protected static string | BackedEnum | null $navigationIcon = 'heroicon-o-adjustments-horizontal';
|
||||
protected static string | UnitEnum | null $navigationGroup = 'Catalog';
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-adjustments-horizontal';
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Catalog';
|
||||
|
||||
protected static ?int $navigationSort = 30;
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
@ -35,21 +37,7 @@ class ListingCustomFieldResource extends Resource
|
||||
->maxLength(255)
|
||||
->live(onBlur: true)
|
||||
->afterStateUpdated(function ($state, $set, ?ListingCustomField $record): void {
|
||||
$baseName = \Illuminate\Support\Str::slug((string) $state, '_');
|
||||
$baseName = $baseName !== '' ? $baseName : 'custom_field';
|
||||
|
||||
$name = $baseName;
|
||||
$counter = 1;
|
||||
|
||||
while (ListingCustomField::query()
|
||||
->where('name', $name)
|
||||
->when($record, fn ($query) => $query->whereKeyNot($record->getKey()))
|
||||
->exists()) {
|
||||
$name = "{$baseName}_{$counter}";
|
||||
$counter++;
|
||||
}
|
||||
|
||||
$set('name', $name);
|
||||
$set('name', ListingCustomField::uniqueNameFromLabel((string) $state, $record));
|
||||
}),
|
||||
TextInput::make('name')
|
||||
->required()
|
||||
@ -63,11 +51,7 @@ class ListingCustomFieldResource extends Resource
|
||||
->live(),
|
||||
Select::make('category_id')
|
||||
->label('Category')
|
||||
->options(fn (): array => Category::query()
|
||||
->where('is_active', true)
|
||||
->orderBy('name')
|
||||
->pluck('name', 'id')
|
||||
->all())
|
||||
->options(fn (): array => Category::activeIdNameOptions())
|
||||
->searchable()
|
||||
->preload()
|
||||
->nullable()
|
||||
@ -106,10 +90,7 @@ class ListingCustomFieldResource extends Resource
|
||||
TextColumn::make('sort_order')->sortable(),
|
||||
])
|
||||
->defaultSort('id', 'desc')
|
||||
->actions([
|
||||
EditAction::make(),
|
||||
DeleteAction::make(),
|
||||
]);
|
||||
->actions(ResourceTableActions::editDelete());
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
@ -1,9 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Admin\Filament\Resources\ListingCustomFieldResource\Pages;
|
||||
namespace Modules\Listing\Filament\Admin\Resources\ListingCustomFieldResource\Pages;
|
||||
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Modules\Admin\Filament\Resources\ListingCustomFieldResource;
|
||||
use Modules\Listing\Filament\Admin\Resources\ListingCustomFieldResource;
|
||||
|
||||
class CreateListingCustomField extends CreateRecord
|
||||
{
|
||||
@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Admin\Filament\Resources\ListingCustomFieldResource\Pages;
|
||||
namespace Modules\Listing\Filament\Admin\Resources\ListingCustomFieldResource\Pages;
|
||||
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Modules\Admin\Filament\Resources\ListingCustomFieldResource;
|
||||
use Modules\Listing\Filament\Admin\Resources\ListingCustomFieldResource;
|
||||
|
||||
class EditListingCustomField extends EditRecord
|
||||
{
|
||||
@ -1,10 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Admin\Filament\Resources\ListingCustomFieldResource\Pages;
|
||||
namespace Modules\Listing\Filament\Admin\Resources\ListingCustomFieldResource\Pages;
|
||||
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Modules\Admin\Filament\Resources\ListingCustomFieldResource;
|
||||
use Modules\Listing\Filament\Admin\Resources\ListingCustomFieldResource;
|
||||
|
||||
class ListListingCustomFields extends ListRecords
|
||||
{
|
||||
41
Modules/Listing/Filament/Admin/Resources/ListingResource.php
Normal file
41
Modules/Listing/Filament/Admin/Resources/ListingResource.php
Normal file
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Listing\Filament\Admin\Resources;
|
||||
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Tables\Table;
|
||||
use Modules\Listing\Filament\Admin\Resources\ListingResource\Pages;
|
||||
use Modules\Listing\Models\Listing;
|
||||
use Modules\Listing\Support\Filament\AdminListingResourceSchema;
|
||||
use UnitEnum;
|
||||
|
||||
class ListingResource extends Resource
|
||||
{
|
||||
protected static ?string $model = Listing::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-clipboard-document-list';
|
||||
|
||||
protected static string|UnitEnum|null $navigationGroup = 'Catalog';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return $schema->schema(AdminListingResourceSchema::form());
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return AdminListingResourceSchema::configureTable($table, static::class);
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListListings::route('/'),
|
||||
'create' => Pages\CreateListing::route('/create'),
|
||||
'activities' => Pages\ListListingActivities::route('/{record}/activities'),
|
||||
'edit' => Pages\EditListing::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,9 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources\ListingResource\Pages;
|
||||
|
||||
namespace Modules\Listing\Filament\Admin\Resources\ListingResource\Pages;
|
||||
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
use Modules\Admin\Filament\Resources\ListingResource;
|
||||
use Modules\Listing\Filament\Admin\Resources\ListingResource;
|
||||
|
||||
class CreateListing extends CreateRecord
|
||||
{
|
||||
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Listing\Filament\Admin\Resources\ListingResource\Pages;
|
||||
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
use Modules\Listing\Filament\Admin\Resources\ListingResource;
|
||||
|
||||
class EditListing extends EditRecord
|
||||
{
|
||||
protected static string $resource = ListingResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [DeleteAction::make()];
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,8 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Resources\ListingResource\Pages;
|
||||
|
||||
use Modules\Admin\Filament\Resources\ListingResource;
|
||||
namespace Modules\Listing\Filament\Admin\Resources\ListingResource\Pages;
|
||||
|
||||
use Modules\Listing\Filament\Admin\Resources\ListingResource;
|
||||
use pxlrbt\FilamentActivityLog\Pages\ListActivities;
|
||||
|
||||
class ListListingActivities extends ListActivities
|
||||
@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Listing\Filament\Admin\Resources\ListingResource\Pages;
|
||||
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
use Modules\Listing\Filament\Admin\Resources\ListingResource;
|
||||
|
||||
class ListListings extends ListRecords
|
||||
{
|
||||
protected static string $resource = ListingResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [CreateAction::make()];
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Widgets;
|
||||
|
||||
namespace Modules\Listing\Filament\Admin\Widgets;
|
||||
|
||||
use Filament\Widgets\StatsOverviewWidget;
|
||||
use Filament\Widgets\StatsOverviewWidget\Stat;
|
||||
@ -13,31 +14,27 @@ class ListingOverview extends StatsOverviewWidget
|
||||
|
||||
protected function getStats(): array
|
||||
{
|
||||
$totalListings = Listing::query()->count();
|
||||
$activeListings = Listing::query()->where('status', 'active')->count();
|
||||
$pendingListings = Listing::query()->where('status', 'pending')->count();
|
||||
$featuredListings = Listing::query()->where('is_featured', true)->count();
|
||||
$createdToday = Listing::query()->where('created_at', '>=', now()->startOfDay())->count();
|
||||
$stats = Listing::overviewStats();
|
||||
|
||||
$featuredRatio = $totalListings > 0
|
||||
? number_format(($featuredListings / $totalListings) * 100, 1).'% of all listings'
|
||||
$featuredRatio = $stats['total'] > 0
|
||||
? number_format(($stats['featured'] / $stats['total']) * 100, 1).'% of all listings'
|
||||
: '0.0% of all listings';
|
||||
|
||||
return [
|
||||
Stat::make('Total Listings', number_format($totalListings))
|
||||
Stat::make('Total Listings', number_format($stats['total']))
|
||||
->description('All listings in the system')
|
||||
->icon('heroicon-o-clipboard-document-list')
|
||||
->color('primary'),
|
||||
Stat::make('Active Listings', number_format($activeListings))
|
||||
->description(number_format($pendingListings).' pending review')
|
||||
Stat::make('Active Listings', number_format($stats['active']))
|
||||
->description(number_format($stats['pending']).' pending review')
|
||||
->descriptionIcon('heroicon-o-clock')
|
||||
->icon('heroicon-o-check-circle')
|
||||
->color('success'),
|
||||
Stat::make('Created Today', number_format($createdToday))
|
||||
Stat::make('Created Today', number_format($stats['created_today']))
|
||||
->description('New listings added today')
|
||||
->icon('heroicon-o-calendar-days')
|
||||
->color('info'),
|
||||
Stat::make('Featured Listings', number_format($featuredListings))
|
||||
Stat::make('Featured Listings', number_format($stats['featured']))
|
||||
->description($featuredRatio)
|
||||
->icon('heroicon-o-star')
|
||||
->color('warning'),
|
||||
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
namespace Modules\Admin\Filament\Widgets;
|
||||
|
||||
namespace Modules\Listing\Filament\Admin\Widgets;
|
||||
|
||||
use Filament\Widgets\ChartWidget;
|
||||
use Modules\Listing\Models\Listing;
|
||||
@ -8,6 +9,8 @@ class ListingsTrendChart extends ChartWidget
|
||||
{
|
||||
protected static ?int $sort = 2;
|
||||
|
||||
protected int|string|array $columnSpan = 'full';
|
||||
|
||||
protected ?string $heading = 'Listing Creation Trend';
|
||||
|
||||
protected ?string $description = 'Daily listing volume by selected period.';
|
||||
@ -24,39 +27,20 @@ class ListingsTrendChart extends ChartWidget
|
||||
protected function getData(): array
|
||||
{
|
||||
$days = (int) ($this->filter ?? '30');
|
||||
$startDate = now()->startOfDay()->subDays($days - 1);
|
||||
|
||||
$countsByDate = Listing::query()
|
||||
->selectRaw('DATE(created_at) as day, COUNT(*) as total')
|
||||
->where('created_at', '>=', $startDate)
|
||||
->groupBy('day')
|
||||
->orderBy('day')
|
||||
->pluck('total', 'day')
|
||||
->all();
|
||||
|
||||
$labels = [];
|
||||
$data = [];
|
||||
|
||||
for ($index = 0; $index < $days; $index++) {
|
||||
$date = $startDate->copy()->addDays($index);
|
||||
$dateKey = $date->toDateString();
|
||||
|
||||
$labels[] = $date->format('M j');
|
||||
$data[] = (int) ($countsByDate[$dateKey] ?? 0);
|
||||
}
|
||||
$trend = Listing::creationTrend($days);
|
||||
|
||||
return [
|
||||
'datasets' => [
|
||||
[
|
||||
'label' => 'Listings',
|
||||
'data' => $data,
|
||||
'data' => $trend['data'],
|
||||
'fill' => true,
|
||||
'borderColor' => '#2563eb',
|
||||
'backgroundColor' => 'rgba(37, 99, 235, 0.12)',
|
||||
'tension' => 0.35,
|
||||
],
|
||||
],
|
||||
'labels' => $labels,
|
||||
'labels' => $trend['labels'],
|
||||
];
|
||||
}
|
||||
|
||||
@ -1,18 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Listing\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Modules\Conversation\App\Models\Conversation;
|
||||
use Modules\Favorite\App\Models\FavoriteSearch;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Modules\Location\Models\City;
|
||||
use Modules\Location\Models\Country;
|
||||
use Modules\Category\Models\Category;
|
||||
use Modules\Listing\Models\Listing;
|
||||
use Modules\Listing\Support\ListingCustomFieldSchemaBuilder;
|
||||
use Modules\Location\Models\Country;
|
||||
use Modules\Theme\Support\ThemeManager;
|
||||
use Throwable;
|
||||
|
||||
class ListingController extends Controller
|
||||
{
|
||||
@ -33,6 +30,9 @@ class ListingController extends Controller
|
||||
$cityId = request()->integer('city');
|
||||
$cityId = $cityId > 0 ? $cityId : null;
|
||||
|
||||
$sellerUserId = request()->integer('user');
|
||||
$sellerUserId = $sellerUserId > 0 ? $sellerUserId : null;
|
||||
|
||||
$minPriceInput = trim((string) request('min_price', ''));
|
||||
$maxPriceInput = trim((string) request('max_price', ''));
|
||||
$minPrice = is_numeric($minPriceInput) ? max((float) $minPriceInput, 0) : null;
|
||||
@ -50,19 +50,13 @@ class ListingController extends Controller
|
||||
$sort = 'smart';
|
||||
}
|
||||
|
||||
$countries = collect();
|
||||
$cities = collect();
|
||||
$selectedCountryName = null;
|
||||
$selectedCityName = null;
|
||||
|
||||
$this->resolveLocationFilters(
|
||||
$countryId,
|
||||
$cityId,
|
||||
$countries,
|
||||
$cities,
|
||||
$selectedCountryName,
|
||||
$selectedCityName
|
||||
);
|
||||
$locationSelection = Country::browseSelection($countryId, $cityId);
|
||||
$countryId = $locationSelection['country_id'];
|
||||
$cityId = $locationSelection['city_id'];
|
||||
$countries = $locationSelection['countries'];
|
||||
$cities = $locationSelection['cities'];
|
||||
$selectedCountryName = $locationSelection['selected_country_name'];
|
||||
$selectedCityName = $locationSelection['selected_city_name'];
|
||||
|
||||
$listingDirectory = Category::listingDirectory($categoryId);
|
||||
|
||||
@ -70,6 +64,7 @@ class ListingController extends Controller
|
||||
'search' => $search,
|
||||
'country' => $selectedCountryName,
|
||||
'city' => $selectedCityName,
|
||||
'user_id' => $sellerUserId,
|
||||
'min_price' => $minPrice,
|
||||
'max_price' => $maxPrice,
|
||||
'date_filter' => $dateFilter,
|
||||
@ -105,29 +100,13 @@ class ListingController extends Controller
|
||||
if (auth()->check()) {
|
||||
$userId = (int) auth()->id();
|
||||
|
||||
$favoriteListingIds = auth()->user()
|
||||
->favoriteListings()
|
||||
->pluck('listings.id')
|
||||
->all();
|
||||
$favoriteListingIds = auth()->user()->favoriteListingIds();
|
||||
$conversationListingMap = Conversation::listingMapForBuyer($userId);
|
||||
|
||||
$conversationListingMap = Conversation::query()
|
||||
->where('buyer_id', $userId)
|
||||
->pluck('id', 'listing_id')
|
||||
->map(fn ($conversationId) => (int) $conversationId)
|
||||
->all();
|
||||
|
||||
$filters = FavoriteSearch::normalizeFilters([
|
||||
$isCurrentSearchSaved = FavoriteSearch::isSavedForUser(auth()->user(), [
|
||||
'search' => $search,
|
||||
'category' => $categoryId,
|
||||
]);
|
||||
|
||||
if ($filters !== []) {
|
||||
$signature = FavoriteSearch::signatureFor($filters);
|
||||
$isCurrentSearchSaved = auth()->user()
|
||||
->favoriteSearches()
|
||||
->where('signature', $signature)
|
||||
->exists();
|
||||
}
|
||||
}
|
||||
|
||||
return view($this->themes->view('listing', 'index'), compact(
|
||||
@ -136,6 +115,7 @@ class ListingController extends Controller
|
||||
'categoryId',
|
||||
'countryId',
|
||||
'cityId',
|
||||
'sellerUserId',
|
||||
'minPriceInput',
|
||||
'maxPriceInput',
|
||||
'dateFilter',
|
||||
@ -154,13 +134,7 @@ class ListingController extends Controller
|
||||
|
||||
public function show(Listing $listing)
|
||||
{
|
||||
if (
|
||||
Schema::hasColumn('listings', 'view_count')
|
||||
&& (! auth()->check() || (int) auth()->id() !== (int) $listing->user_id)
|
||||
) {
|
||||
$listing->increment('view_count');
|
||||
$listing->refresh();
|
||||
}
|
||||
$listing->trackViewBy(auth()->id());
|
||||
|
||||
$listing->loadMissing([
|
||||
'user:id,name,email',
|
||||
@ -173,7 +147,7 @@ class ListingController extends Controller
|
||||
$listing->category_id ? (int) $listing->category_id : null,
|
||||
$listing->custom_fields ?? [],
|
||||
);
|
||||
$gallery = $listing->themeGallery();
|
||||
$gallery = $listing->galleryImageData();
|
||||
$listingVideos = $listing->getRelation('videos');
|
||||
$relatedListings = $listing->relatedSuggestions(12);
|
||||
$themePillCategories = Category::themePills(10);
|
||||
@ -183,15 +157,12 @@ class ListingController extends Controller
|
||||
|
||||
$isListingFavorited = false;
|
||||
$isSellerFavorited = false;
|
||||
$existingConversationId = null;
|
||||
$detailConversation = null;
|
||||
|
||||
if (auth()->check()) {
|
||||
$userId = (int) auth()->id();
|
||||
|
||||
$isListingFavorited = auth()->user()
|
||||
->favoriteListings()
|
||||
->whereKey($listing->getKey())
|
||||
->exists();
|
||||
$isListingFavorited = in_array((int) $listing->getKey(), auth()->user()->favoriteListingIds(), true);
|
||||
|
||||
if ($listing->user_id) {
|
||||
$isSellerFavorited = auth()->user()
|
||||
@ -201,7 +172,7 @@ class ListingController extends Controller
|
||||
}
|
||||
|
||||
if ($listing->user_id && (int) $listing->user_id !== $userId) {
|
||||
$existingConversationId = Conversation::buyerListingConversationId(
|
||||
$detailConversation = Conversation::detailForBuyerListing(
|
||||
(int) $listing->getKey(),
|
||||
$userId,
|
||||
);
|
||||
@ -213,7 +184,7 @@ class ListingController extends Controller
|
||||
'isListingFavorited',
|
||||
'isSellerFavorited',
|
||||
'presentableCustomFields',
|
||||
'existingConversationId',
|
||||
'detailConversation',
|
||||
'gallery',
|
||||
'listingVideos',
|
||||
'relatedListings',
|
||||
@ -239,83 +210,6 @@ class ListingController extends Controller
|
||||
|
||||
return redirect()
|
||||
->route('panel.listings.create')
|
||||
->with('success', 'İlan oluşturma ekranına yönlendirildin.');
|
||||
->with('success', 'You were redirected to the listing creation screen.');
|
||||
}
|
||||
|
||||
private function resolveLocationFilters(
|
||||
?int &$countryId,
|
||||
?int &$cityId,
|
||||
Collection &$countries,
|
||||
Collection &$cities,
|
||||
?string &$selectedCountryName,
|
||||
?string &$selectedCityName
|
||||
): void {
|
||||
try {
|
||||
if (! Schema::hasTable('countries') || ! Schema::hasTable('cities')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$countries = Country::query()
|
||||
->where('is_active', true)
|
||||
->orderBy('name')
|
||||
->get(['id', 'name']);
|
||||
|
||||
$selectedCountry = $countryId
|
||||
? $countries->firstWhere('id', $countryId)
|
||||
: null;
|
||||
|
||||
if (! $selectedCountry && $countryId) {
|
||||
$selectedCountry = Country::query()->whereKey($countryId)->first(['id', 'name']);
|
||||
}
|
||||
|
||||
$selectedCity = null;
|
||||
if ($cityId) {
|
||||
$selectedCity = City::query()->whereKey($cityId)->first(['id', 'name', 'country_id']);
|
||||
if (! $selectedCity) {
|
||||
$cityId = null;
|
||||
}
|
||||
}
|
||||
|
||||
if ($selectedCity && ! $selectedCountry) {
|
||||
$countryId = (int) $selectedCity->country_id;
|
||||
$selectedCountry = Country::query()->whereKey($countryId)->first(['id', 'name']);
|
||||
}
|
||||
|
||||
if ($selectedCountry) {
|
||||
$selectedCountryName = (string) $selectedCountry->name;
|
||||
$cities = City::query()
|
||||
->where('country_id', $selectedCountry->id)
|
||||
->where('is_active', true)
|
||||
->orderBy('name')
|
||||
->get(['id', 'name', 'country_id']);
|
||||
|
||||
if ($cities->isEmpty()) {
|
||||
$cities = City::query()
|
||||
->where('country_id', $selectedCountry->id)
|
||||
->orderBy('name')
|
||||
->get(['id', 'name', 'country_id']);
|
||||
}
|
||||
} else {
|
||||
$countryId = null;
|
||||
$cities = collect();
|
||||
}
|
||||
|
||||
if ($selectedCity) {
|
||||
if ($selectedCountry && (int) $selectedCity->country_id !== (int) $selectedCountry->id) {
|
||||
$selectedCity = null;
|
||||
$cityId = null;
|
||||
} else {
|
||||
$selectedCityName = (string) $selectedCity->name;
|
||||
}
|
||||
}
|
||||
} catch (Throwable) {
|
||||
$countryId = null;
|
||||
$cityId = null;
|
||||
$selectedCountryName = null;
|
||||
$selectedCityName = null;
|
||||
$countries = collect();
|
||||
$cities = collect();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
34
Modules/Listing/ListingPlugin.php
Normal file
34
Modules/Listing/ListingPlugin.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Listing;
|
||||
|
||||
use Filament\Contracts\Plugin;
|
||||
use Filament\Panel;
|
||||
|
||||
final class ListingPlugin implements Plugin
|
||||
{
|
||||
public function getId(): string
|
||||
{
|
||||
return 'listing';
|
||||
}
|
||||
|
||||
public static function make(): static
|
||||
{
|
||||
return app(static::class);
|
||||
}
|
||||
|
||||
public function register(Panel $panel): void
|
||||
{
|
||||
$panel
|
||||
->discoverResources(
|
||||
in: module_path('Listing', 'Filament/Admin/Resources'),
|
||||
for: 'Modules\\Listing\\Filament\\Admin\\Resources',
|
||||
)
|
||||
->discoverWidgets(
|
||||
in: module_path('Listing', 'Filament/Admin/Widgets'),
|
||||
for: 'Modules\\Listing\\Filament\\Admin\\Widgets',
|
||||
);
|
||||
}
|
||||
|
||||
public function boot(Panel $panel): void {}
|
||||
}
|
||||
@ -1,23 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Listing\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Casts\Attribute;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Category\Models\Category;
|
||||
use Modules\Conversation\App\Models\Conversation;
|
||||
use Modules\Listing\States\ListingStatus;
|
||||
use Modules\Listing\Support\ListingImageViewData;
|
||||
use Modules\Listing\Support\ListingPanelHelper;
|
||||
use Modules\Site\App\Support\LocalMedia;
|
||||
use Modules\User\App\Models\User;
|
||||
use Modules\Video\Enums\VideoStatus;
|
||||
use Modules\Video\Models\Video;
|
||||
use Spatie\Activitylog\LogOptions;
|
||||
use Spatie\Activitylog\Traits\LogsActivity;
|
||||
use Spatie\Image\Enums\Fit;
|
||||
use Spatie\MediaLibrary\HasMedia;
|
||||
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
use Spatie\ModelStates\HasStates;
|
||||
use Throwable;
|
||||
|
||||
class Listing extends Model implements HasMedia
|
||||
{
|
||||
@ -56,23 +65,23 @@ class Listing extends Model implements HasMedia
|
||||
|
||||
public function category()
|
||||
{
|
||||
return $this->belongsTo(\Modules\Category\Models\Category::class);
|
||||
return $this->belongsTo(Category::class);
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(\Modules\User\App\Models\User::class);
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function favoritedByUsers()
|
||||
{
|
||||
return $this->belongsToMany(\Modules\User\App\Models\User::class, 'favorite_listings')
|
||||
return $this->belongsToMany(User::class, 'favorite_listings')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function conversations()
|
||||
{
|
||||
return $this->hasMany(\Modules\Conversation\App\Models\Conversation::class);
|
||||
return $this->hasMany(Conversation::class);
|
||||
}
|
||||
|
||||
public function videos()
|
||||
@ -93,7 +102,7 @@ class Listing extends Model implements HasMedia
|
||||
return $query->where('status', 'active');
|
||||
}
|
||||
|
||||
public function scopeOwnedByUser(Builder $query, int | string | null $userId): Builder
|
||||
public function scopeOwnedByUser(Builder $query, int|string|null $userId): Builder
|
||||
{
|
||||
return $query->where('user_id', $userId);
|
||||
}
|
||||
@ -123,6 +132,24 @@ class Listing extends Model implements HasMedia
|
||||
});
|
||||
}
|
||||
|
||||
public function scopeWithPanelIndexState(Builder $query): Builder
|
||||
{
|
||||
return $query
|
||||
->with('category:id,name')
|
||||
->withCount('favoritedByUsers')
|
||||
->withCount('videos')
|
||||
->withCount([
|
||||
'videos as ready_videos_count' => fn (Builder $videoQuery): Builder => $videoQuery
|
||||
->whereNotNull('path')
|
||||
->where('is_active', true),
|
||||
'videos as pending_videos_count' => fn (Builder $videoQuery): Builder => $videoQuery
|
||||
->whereIn('status', [
|
||||
VideoStatus::Pending->value,
|
||||
VideoStatus::Processing->value,
|
||||
]),
|
||||
]);
|
||||
}
|
||||
|
||||
public function scopeForCategory(Builder $query, ?int $categoryId): Builder
|
||||
{
|
||||
return $query->forCategoryIds(Category::listingFilterIds($categoryId));
|
||||
@ -146,6 +173,7 @@ class Listing extends Model implements HasMedia
|
||||
$search = trim((string) ($filters['search'] ?? ''));
|
||||
$country = isset($filters['country']) ? trim((string) $filters['country']) : null;
|
||||
$city = isset($filters['city']) ? trim((string) $filters['city']) : null;
|
||||
$userId = isset($filters['user_id']) && is_numeric($filters['user_id']) ? (int) $filters['user_id'] : null;
|
||||
$minPrice = is_numeric($filters['min_price'] ?? null) ? max((float) $filters['min_price'], 0) : null;
|
||||
$maxPrice = is_numeric($filters['max_price'] ?? null) ? max((float) $filters['max_price'], 0) : null;
|
||||
$dateFilter = (string) ($filters['date_filter'] ?? 'all');
|
||||
@ -154,6 +182,7 @@ class Listing extends Model implements HasMedia
|
||||
$query
|
||||
->searchTerm($search)
|
||||
->forCategoryIds(is_array($categoryIds) ? $categoryIds : null)
|
||||
->when(! is_null($userId) && $userId > 0, fn (Builder $builder) => $builder->where('user_id', $userId))
|
||||
->when($country !== null && $country !== '', fn (Builder $builder) => $builder->where('country', $country))
|
||||
->when($city !== null && $city !== '', fn (Builder $builder) => $builder->where('city', $city))
|
||||
->when(! is_null($minPrice), fn (Builder $builder) => $builder->whereNotNull('price')->where('price', '>=', $minPrice))
|
||||
@ -180,22 +209,56 @@ class Listing extends Model implements HasMedia
|
||||
|
||||
public function themeGallery(): array
|
||||
{
|
||||
$mediaUrls = $this->getMedia('listing-images')
|
||||
->map(fn ($media): string => $media->getUrl())
|
||||
->filter(fn (string $url): bool => $url !== '')
|
||||
return collect($this->galleryImageData())
|
||||
->map(fn (array $image): ?string => ListingImageViewData::pickUrl($image['gallery'] ?? null))
|
||||
->filter(fn (?string $url): bool => is_string($url) && $url !== '')
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
if ($mediaUrls !== []) {
|
||||
return $mediaUrls;
|
||||
public function galleryImageData(): array
|
||||
{
|
||||
$mediaItems = $this->getMedia('listing-images');
|
||||
|
||||
if ($mediaItems->isNotEmpty()) {
|
||||
return $mediaItems
|
||||
->map(fn (Media $media): array => [
|
||||
'gallery' => ListingImageViewData::fromMedia($media, 'gallery'),
|
||||
'thumb' => ListingImageViewData::fromMedia($media, 'thumb'),
|
||||
])
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
return collect($this->images ?? [])
|
||||
->filter(fn ($value): bool => is_string($value) && trim($value) !== '')
|
||||
->map(fn (string $url): array => [
|
||||
'gallery' => ListingImageViewData::fromUrl($url),
|
||||
'thumb' => ListingImageViewData::fromUrl($url),
|
||||
])
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
public function primaryImageData(string $context = 'card'): ?array
|
||||
{
|
||||
$media = $this->getFirstMedia('listing-images');
|
||||
|
||||
if ($media instanceof Media) {
|
||||
return ListingImageViewData::fromMedia($media, $context);
|
||||
}
|
||||
|
||||
$fallback = collect($this->images ?? [])
|
||||
->first(fn ($value): bool => is_string($value) && trim($value) !== '');
|
||||
|
||||
return ListingImageViewData::fromUrl(is_string($fallback) ? $fallback : null);
|
||||
}
|
||||
|
||||
public function primaryImageUrl(string $context = 'card', string $viewport = 'desktop'): ?string
|
||||
{
|
||||
return ListingImageViewData::pickUrl($this->primaryImageData($context), $viewport);
|
||||
}
|
||||
|
||||
public function relatedSuggestions(int $limit = 8): Collection
|
||||
{
|
||||
$baseQuery = static::query()
|
||||
@ -232,7 +295,7 @@ class Listing extends Model implements HasMedia
|
||||
];
|
||||
}
|
||||
|
||||
public static function panelStatusCountsForUser(int | string $userId): array
|
||||
public static function panelStatusCountsForUser(int|string $userId): array
|
||||
{
|
||||
$counts = static::query()
|
||||
->ownedByUser($userId)
|
||||
@ -249,17 +312,106 @@ class Listing extends Model implements HasMedia
|
||||
];
|
||||
}
|
||||
|
||||
public static function activeCount(): int
|
||||
{
|
||||
return (int) static::query()
|
||||
->active()
|
||||
->count();
|
||||
}
|
||||
|
||||
public static function overviewStats(): array
|
||||
{
|
||||
$counts = static::query()
|
||||
->selectRaw('COUNT(*) as total')
|
||||
->selectRaw("SUM(CASE WHEN status = 'active' THEN 1 ELSE 0 END) as active")
|
||||
->selectRaw("SUM(CASE WHEN status = 'pending' THEN 1 ELSE 0 END) as pending")
|
||||
->selectRaw('SUM(CASE WHEN is_featured = true THEN 1 ELSE 0 END) as featured')
|
||||
->first();
|
||||
|
||||
return [
|
||||
'total' => (int) ($counts?->total ?? 0),
|
||||
'active' => (int) ($counts?->active ?? 0),
|
||||
'pending' => (int) ($counts?->pending ?? 0),
|
||||
'featured' => (int) ($counts?->featured ?? 0),
|
||||
'created_today' => (int) static::query()
|
||||
->where('created_at', '>=', now()->startOfDay())
|
||||
->count(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function creationTrend(int $days): array
|
||||
{
|
||||
$safeDays = max(1, $days);
|
||||
$startDate = now()->startOfDay()->subDays($safeDays - 1);
|
||||
$countsByDate = static::query()
|
||||
->selectRaw('DATE(created_at) as day, COUNT(*) as total')
|
||||
->where('created_at', '>=', $startDate)
|
||||
->groupBy('day')
|
||||
->orderBy('day')
|
||||
->pluck('total', 'day')
|
||||
->all();
|
||||
$labels = [];
|
||||
$data = [];
|
||||
|
||||
for ($index = 0; $index < $safeDays; $index++) {
|
||||
$date = $startDate->copy()->addDays($index);
|
||||
$dateKey = $date->toDateString();
|
||||
|
||||
$labels[] = $date->format('M j');
|
||||
$data[] = (int) ($countsByDate[$dateKey] ?? 0);
|
||||
}
|
||||
|
||||
return [
|
||||
'labels' => $labels,
|
||||
'data' => $data,
|
||||
];
|
||||
}
|
||||
|
||||
public static function homeFeatured(int $limit = 4): Collection
|
||||
{
|
||||
return static::query()
|
||||
->active()
|
||||
->where('is_featured', true)
|
||||
->latest()
|
||||
->take($limit)
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function homeRecent(int $limit = 8): Collection
|
||||
{
|
||||
return static::query()
|
||||
->active()
|
||||
->latest()
|
||||
->take($limit)
|
||||
->get();
|
||||
}
|
||||
|
||||
public static function panelIndexDataForUser(User $user, string $search, string $status): array
|
||||
{
|
||||
$listings = static::query()
|
||||
->ownedByUser($user->getKey())
|
||||
->withPanelIndexState()
|
||||
->searchTerm($search)
|
||||
->forPanelStatus($status)
|
||||
->latest('id')
|
||||
->paginate(10)
|
||||
->withQueryString();
|
||||
|
||||
return [
|
||||
'listings' => $listings,
|
||||
'counts' => static::panelStatusCountsForUser($user->getKey()),
|
||||
];
|
||||
}
|
||||
|
||||
public function panelPrimaryImageUrl(): ?string
|
||||
{
|
||||
$url = trim((string) $this->getFirstMediaUrl('listing-images'));
|
||||
|
||||
return $url !== '' ? $url : null;
|
||||
return $this->primaryImageUrl('card', 'desktop');
|
||||
}
|
||||
|
||||
public function panelPriceLabel(): string
|
||||
{
|
||||
if (is_null($this->price)) {
|
||||
return 'Ücretsiz';
|
||||
return 'Free';
|
||||
}
|
||||
|
||||
return number_format((float) $this->price, 2, ',', '.').' '.($this->currency ?? 'TL');
|
||||
@ -269,24 +421,24 @@ class Listing extends Model implements HasMedia
|
||||
{
|
||||
return match ($this->statusValue()) {
|
||||
'sold' => [
|
||||
'label' => 'Satıldı',
|
||||
'label' => 'Sold',
|
||||
'badge_class' => 'is-success',
|
||||
'hint' => 'İlan satıldı olarak işaretlendi.',
|
||||
'hint' => 'This listing is marked as sold.',
|
||||
],
|
||||
'expired' => [
|
||||
'label' => 'Süresi doldu',
|
||||
'label' => 'Expired',
|
||||
'badge_class' => 'is-danger',
|
||||
'hint' => 'Yeniden yayına alınmayı bekliyor.',
|
||||
'hint' => 'This listing is waiting to be republished.',
|
||||
],
|
||||
'pending' => [
|
||||
'label' => 'İncelemede',
|
||||
'label' => 'Pending review',
|
||||
'badge_class' => 'is-warning',
|
||||
'hint' => 'Moderasyon onayı bekleniyor.',
|
||||
'hint' => 'Waiting for moderation approval.',
|
||||
],
|
||||
default => [
|
||||
'label' => 'Yayında',
|
||||
'label' => 'Live',
|
||||
'badge_class' => 'is-primary',
|
||||
'hint' => 'Şu anda ziyaretçilere görünüyor.',
|
||||
'hint' => 'Visible to visitors right now.',
|
||||
],
|
||||
};
|
||||
}
|
||||
@ -298,7 +450,7 @@ class Listing extends Model implements HasMedia
|
||||
trim((string) $this->country),
|
||||
])->filter()->values();
|
||||
|
||||
return $parts->isNotEmpty() ? $parts->implode(', ') : 'Konum belirtilmedi';
|
||||
return $parts->isNotEmpty() ? $parts->implode(', ') : 'Location not specified';
|
||||
}
|
||||
|
||||
public function panelPublishedAt(): ?Carbon
|
||||
@ -320,16 +472,16 @@ class Listing extends Model implements HasMedia
|
||||
public function panelExpirySummary(): string
|
||||
{
|
||||
if (! $this->expires_at) {
|
||||
return 'Süre sınırı yok';
|
||||
return 'No expiry limit';
|
||||
}
|
||||
|
||||
$expiresAt = $this->expires_at->copy()->startOfDay();
|
||||
$days = Carbon::today()->diffInDays($expiresAt, false);
|
||||
|
||||
return match (true) {
|
||||
$days > 0 => $days.' gün kaldı',
|
||||
$days === 0 => 'Bugün sona eriyor',
|
||||
default => abs($days).' gün önce sona erdi',
|
||||
$days > 0 => $days.' days left',
|
||||
$days === 0 => 'Ends today',
|
||||
default => 'Expired '.abs($days).' days ago',
|
||||
};
|
||||
}
|
||||
|
||||
@ -340,11 +492,60 @@ class Listing extends Model implements HasMedia
|
||||
}
|
||||
|
||||
return [
|
||||
'label' => $total.' video',
|
||||
'detail' => $ready.' hazır'.($pending > 0 ? ', '.$pending.' işleniyor' : ''),
|
||||
'label' => $total.' videos',
|
||||
'detail' => $ready.' ready'.($pending > 0 ? ', '.$pending.' processing' : ''),
|
||||
];
|
||||
}
|
||||
|
||||
public function replacePublicImage(string $absolutePath, ?string $fileName = null): void
|
||||
{
|
||||
if (! is_file($absolutePath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$disk = $this->mediaDisk();
|
||||
$targetFileName = trim((string) ($fileName ?: basename($absolutePath)));
|
||||
$existingMediaItems = $this->getMedia('listing-images');
|
||||
|
||||
if ($existingMediaItems->count() === 1) {
|
||||
$existingMedia = $existingMediaItems->first();
|
||||
|
||||
if (
|
||||
$existingMedia
|
||||
&& (string) $existingMedia->file_name === $targetFileName
|
||||
&& (string) $existingMedia->disk === $disk
|
||||
) {
|
||||
try {
|
||||
if (is_file($existingMedia->getPath())) {
|
||||
return;
|
||||
}
|
||||
} catch (Throwable) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->clearMediaCollection('listing-images');
|
||||
$this->attachListingImage($absolutePath, $targetFileName, $disk);
|
||||
}
|
||||
|
||||
public function attachListingImage(string $absolutePath, string $fileName, ?string $disk = null): void
|
||||
{
|
||||
if (! is_file($absolutePath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$targetDisk = is_string($disk) && trim($disk) !== ''
|
||||
? trim($disk)
|
||||
: $this->mediaDisk();
|
||||
|
||||
$this
|
||||
->addMedia($absolutePath)
|
||||
->usingFileName(trim($fileName))
|
||||
->withCustomProperties(self::mediaCustomProperties())
|
||||
->preservingOriginal()
|
||||
->toMediaCollection('listing-images', $targetDisk);
|
||||
}
|
||||
|
||||
public function statusValue(): string
|
||||
{
|
||||
return $this->status instanceof ListingStatus
|
||||
@ -362,6 +563,44 @@ class Listing extends Model implements HasMedia
|
||||
};
|
||||
}
|
||||
|
||||
public function loadPanelEditor(): self
|
||||
{
|
||||
return $this->load([
|
||||
'category:id,name',
|
||||
'videos:id,listing_id,title,status,is_active,path,upload_path,duration_seconds,size',
|
||||
]);
|
||||
}
|
||||
|
||||
public function assertOwnedBy(User $user): void
|
||||
{
|
||||
abort_unless((int) $this->user_id === (int) $user->getKey(), 403);
|
||||
}
|
||||
|
||||
public function trackViewBy(null|int|string $viewerId): void
|
||||
{
|
||||
if ((int) $this->user_id === (int) $viewerId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->increment('view_count');
|
||||
$this->refresh();
|
||||
}
|
||||
|
||||
public function markAsSold(): void
|
||||
{
|
||||
$this->forceFill([
|
||||
'status' => 'sold',
|
||||
])->save();
|
||||
}
|
||||
|
||||
public function republish(): void
|
||||
{
|
||||
$this->forceFill([
|
||||
'status' => 'active',
|
||||
'expires_at' => now()->addDays(self::DEFAULT_PANEL_EXPIRY_WINDOW_DAYS),
|
||||
])->save();
|
||||
}
|
||||
|
||||
public function updateFromPanel(array $attributes): void
|
||||
{
|
||||
$payload = Arr::only($attributes, [
|
||||
@ -387,7 +626,7 @@ class Listing extends Model implements HasMedia
|
||||
$this->forceFill($payload)->save();
|
||||
}
|
||||
|
||||
public static function createFromFrontend(array $data, null | int | string $userId): self
|
||||
public static function createFromFrontend(array $data, null|int|string $userId): self
|
||||
{
|
||||
$baseSlug = Str::slug((string) ($data['title'] ?? 'listing'));
|
||||
$baseSlug = $baseSlug !== '' ? $baseSlug : 'listing';
|
||||
@ -406,7 +645,108 @@ class Listing extends Model implements HasMedia
|
||||
|
||||
public function registerMediaCollections(): void
|
||||
{
|
||||
$this->addMediaCollection('listing-images');
|
||||
$this->addMediaCollection('listing-images')->useDisk($this->mediaDisk());
|
||||
}
|
||||
|
||||
public function registerMediaConversions(?Media $media = null): void
|
||||
{
|
||||
if ($this->shouldSkipConversionsForSeeder()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this
|
||||
->addMediaConversion('gallery-mobile')
|
||||
->fit(Fit::Max, 960, 960)
|
||||
->format('webp')
|
||||
->quality(78)
|
||||
->performOnCollections('listing-images')
|
||||
->nonQueued();
|
||||
|
||||
$this
|
||||
->addMediaConversion('gallery-desktop')
|
||||
->fit(Fit::Max, 1680, 1680)
|
||||
->format('webp')
|
||||
->quality(82)
|
||||
->performOnCollections('listing-images')
|
||||
->nonQueued();
|
||||
|
||||
$this
|
||||
->addMediaConversion('card-mobile')
|
||||
->fit(Fit::Crop, 720, 540)
|
||||
->format('webp')
|
||||
->quality(76)
|
||||
->performOnCollections('listing-images')
|
||||
->nonQueued();
|
||||
|
||||
$this
|
||||
->addMediaConversion('card-desktop')
|
||||
->fit(Fit::Crop, 1080, 810)
|
||||
->format('webp')
|
||||
->quality(80)
|
||||
->performOnCollections('listing-images')
|
||||
->nonQueued();
|
||||
|
||||
$this
|
||||
->addMediaConversion('thumb-mobile')
|
||||
->fit(Fit::Crop, 220, 220)
|
||||
->format('webp')
|
||||
->quality(74)
|
||||
->performOnCollections('listing-images')
|
||||
->nonQueued();
|
||||
|
||||
$this
|
||||
->addMediaConversion('thumb-desktop')
|
||||
->fit(Fit::Crop, 320, 320)
|
||||
->format('webp')
|
||||
->quality(78)
|
||||
->performOnCollections('listing-images')
|
||||
->nonQueued();
|
||||
}
|
||||
|
||||
private function shouldSkipConversionsForSeeder(): bool
|
||||
{
|
||||
if ((bool) config('demo.provisioning', false)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (! app()->runningInConsole()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$argv = implode(' ', (array) ($_SERVER['argv'] ?? []));
|
||||
|
||||
return str_contains($argv, 'db:seed') || str_contains($argv, '--seed');
|
||||
}
|
||||
|
||||
private function mediaDisk(): string
|
||||
{
|
||||
return LocalMedia::disk();
|
||||
}
|
||||
|
||||
public static function mediaCustomProperties(): array
|
||||
{
|
||||
$scope = static::mediaPathScope();
|
||||
|
||||
return $scope !== null
|
||||
? ['path_scope' => $scope]
|
||||
: [];
|
||||
}
|
||||
|
||||
public static function mediaPathScope(): ?string
|
||||
{
|
||||
$connection = (string) config('database.default', 'pgsql');
|
||||
$searchPath = config("database.connections.{$connection}.search_path");
|
||||
$value = is_array($searchPath)
|
||||
? implode('_', $searchPath)
|
||||
: (string) $searchPath;
|
||||
$scope = (string) Str::of($value)
|
||||
->before(',')
|
||||
->trim()
|
||||
->lower()
|
||||
->replaceMatches('/[^a-z0-9_]+/', '_')
|
||||
->trim('_');
|
||||
|
||||
return $scope !== '' ? $scope : null;
|
||||
}
|
||||
|
||||
protected function location(): Attribute
|
||||
|
||||
@ -4,15 +4,21 @@ namespace Modules\Listing\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Category\Models\Category;
|
||||
|
||||
class ListingCustomField extends Model
|
||||
{
|
||||
public const TYPE_TEXT = 'text';
|
||||
|
||||
public const TYPE_TEXTAREA = 'textarea';
|
||||
|
||||
public const TYPE_NUMBER = 'number';
|
||||
|
||||
public const TYPE_SELECT = 'select';
|
||||
|
||||
public const TYPE_BOOLEAN = 'boolean';
|
||||
|
||||
public const TYPE_DATE = 'date';
|
||||
|
||||
protected $fillable = [
|
||||
@ -83,6 +89,24 @@ class ListingCustomField extends Model
|
||||
return collect($options)->mapWithKeys(fn (string $option): array => [$option => $option])->all();
|
||||
}
|
||||
|
||||
public static function uniqueNameFromLabel(string $label, ?self $record = null): string
|
||||
{
|
||||
$baseName = Str::slug($label, '_');
|
||||
$baseName = $baseName !== '' ? $baseName : 'custom_field';
|
||||
$name = $baseName;
|
||||
$counter = 1;
|
||||
|
||||
while (static::query()
|
||||
->where('name', $name)
|
||||
->when($record, fn (Builder $query): Builder => $query->whereKeyNot($record->getKey()))
|
||||
->exists()) {
|
||||
$name = "{$baseName}_{$counter}";
|
||||
$counter++;
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
public static function upsertSeeded(Category $category, array $attributes): self
|
||||
{
|
||||
return static::query()->updateOrCreate(
|
||||
@ -100,4 +124,26 @@ class ListingCustomField extends Model
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
public static function panelFieldDefinitions(?int $categoryId): array
|
||||
{
|
||||
return static::query()
|
||||
->active()
|
||||
->forCategory($categoryId)
|
||||
->ordered()
|
||||
->get(['name', 'label', 'type', 'is_required', 'placeholder', 'help_text', 'options'])
|
||||
->map(fn (self $field): array => [
|
||||
'name' => (string) $field->name,
|
||||
'label' => (string) $field->label,
|
||||
'type' => (string) $field->type,
|
||||
'is_required' => (bool) $field->is_required,
|
||||
'placeholder' => $field->placeholder,
|
||||
'help_text' => $field->help_text,
|
||||
'options' => collect($field->options ?? [])
|
||||
->map(fn ($option): string => (string) $option)
|
||||
->values()
|
||||
->all(),
|
||||
])
|
||||
->all();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Listing\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
@ -6,14 +7,16 @@ use Illuminate\Support\ServiceProvider;
|
||||
class ListingServiceProvider extends ServiceProvider
|
||||
{
|
||||
protected string $moduleName = 'Listing';
|
||||
|
||||
protected string $moduleNameLower = 'listing';
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
$this->loadViewsFrom(module_path($this->moduleName, 'resources/views'), $this->moduleNameLower);
|
||||
$this->loadMigrationsFrom(module_path($this->moduleName, 'database/migrations'));
|
||||
$this->loadMigrationsFrom(module_path($this->moduleName, 'Database/migrations'));
|
||||
$this->loadRoutesFrom(module_path($this->moduleName, 'routes/web.php'));
|
||||
}
|
||||
|
||||
public function register(): void {}
|
||||
public function register(): void
|
||||
{}
|
||||
}
|
||||
|
||||
180
Modules/Listing/Support/Filament/AdminListingResourceSchema.php
Normal file
180
Modules/Listing/Support/Filament/AdminListingResourceSchema.php
Normal file
@ -0,0 +1,180 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Listing\Support\Filament;
|
||||
|
||||
use A909M\FilamentStateFusion\Forms\Components\StateFusionSelect;
|
||||
use A909M\FilamentStateFusion\Tables\Columns\StateFusionSelectColumn;
|
||||
use A909M\FilamentStateFusion\Tables\Filters\StateFusionSelectFilter;
|
||||
use Cheesegrits\FilamentGoogleMaps\Fields\Map;
|
||||
use Filament\Forms\Components\DatePicker;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\SpatieMediaLibraryFileUpload;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Components\Utilities\Get;
|
||||
use Filament\Tables\Columns\IconColumn;
|
||||
use Filament\Tables\Columns\SpatieMediaLibraryImageColumn;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Enums\FiltersLayout;
|
||||
use Filament\Tables\Filters\Filter;
|
||||
use Filament\Tables\Filters\SelectFilter;
|
||||
use Filament\Tables\Filters\TernaryFilter;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Support\Str;
|
||||
use Modules\Admin\Support\Filament\ResourceTableActions;
|
||||
use Modules\Category\Models\Category;
|
||||
use Modules\Listing\Models\Listing;
|
||||
use Modules\Listing\Support\ListingCustomFieldSchemaBuilder;
|
||||
use Modules\Listing\Support\ListingPanelHelper;
|
||||
use Modules\Location\Models\City;
|
||||
use Modules\Location\Models\Country;
|
||||
use Modules\Location\Support\CountryCodeManager;
|
||||
use Modules\Site\App\Support\LocalMedia;
|
||||
use Modules\Video\Support\Filament\VideoFormSchema;
|
||||
use Ysfkaya\FilamentPhoneInput\Forms\PhoneInput;
|
||||
|
||||
class AdminListingResourceSchema
|
||||
{
|
||||
public static function form(): array
|
||||
{
|
||||
return [
|
||||
TextInput::make('title')->required()->maxLength(255)->live(onBlur: true)->afterStateUpdated(fn ($state, $set) => $set('slug', Str::slug($state).'-'.Str::random(4))),
|
||||
TextInput::make('slug')->required()->maxLength(255)->unique(ignoreRecord: true),
|
||||
Textarea::make('description')->rows(4),
|
||||
TextInput::make('price')
|
||||
->numeric()
|
||||
->currencyMask(thousandSeparator: ',', decimalSeparator: '.', precision: 2),
|
||||
Select::make('currency')
|
||||
->options(fn (): array => ListingPanelHelper::currencyOptions())
|
||||
->default(fn (): string => ListingPanelHelper::defaultCurrency())
|
||||
->required(),
|
||||
Select::make('category_id')
|
||||
->label('Category')
|
||||
->options(fn (): array => Category::activeIdNameOptions())
|
||||
->searchable()
|
||||
->live()
|
||||
->afterStateUpdated(fn ($state, $set) => $set('custom_fields', []))
|
||||
->nullable(),
|
||||
Select::make('user_id')->relationship('user', 'email')->label('Owner')->searchable()->preload()->nullable(),
|
||||
Section::make('Custom Fields')
|
||||
->description('Category specific listing attributes.')
|
||||
->schema(fn (Get $get): array => ListingCustomFieldSchemaBuilder::formComponents(
|
||||
($categoryId = $get('category_id')) ? (int) $categoryId : null
|
||||
))
|
||||
->columns(2)
|
||||
->columnSpanFull()
|
||||
->visible(fn (Get $get): bool => ListingCustomFieldSchemaBuilder::hasFields(
|
||||
($categoryId = $get('category_id')) ? (int) $categoryId : null
|
||||
)),
|
||||
StateFusionSelect::make('status')->required(),
|
||||
PhoneInput::make('contact_phone')->defaultCountry(CountryCodeManager::defaultCountryIso2())->nullable(),
|
||||
TextInput::make('contact_email')->email()->maxLength(255),
|
||||
Toggle::make('is_featured')->default(false),
|
||||
Select::make('country')
|
||||
->label('Country')
|
||||
->options(fn (): array => Country::nameOptions())
|
||||
->searchable()
|
||||
->preload()
|
||||
->live()
|
||||
->afterStateUpdated(fn ($state, $set) => $set('city', null))
|
||||
->nullable(),
|
||||
Select::make('city')
|
||||
->label('City')
|
||||
->options(fn (Get $get): array => City::nameOptions($get('country')))
|
||||
->searchable()
|
||||
->preload()
|
||||
->nullable(),
|
||||
Map::make('location')
|
||||
->label('Location')
|
||||
->visible(fn (): bool => ListingPanelHelper::googleMapsEnabled())
|
||||
->draggable()
|
||||
->clickable()
|
||||
->autocomplete('city')
|
||||
->autocompleteReverse(true)
|
||||
->reverseGeocode([
|
||||
'city' => '%L',
|
||||
])
|
||||
->defaultLocation([41.0082, 28.9784])
|
||||
->defaultZoom(10)
|
||||
->height('320px')
|
||||
->columnSpanFull(),
|
||||
SpatieMediaLibraryFileUpload::make('images')
|
||||
->collection('listing-images')
|
||||
->disk(fn (): string => LocalMedia::disk())
|
||||
->customProperties(fn (): array => Listing::mediaCustomProperties())
|
||||
->multiple()
|
||||
->image()
|
||||
->reorderable(),
|
||||
VideoFormSchema::listingSection(),
|
||||
];
|
||||
}
|
||||
|
||||
public static function configureTable(Table $table, string $resourceClass): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
SpatieMediaLibraryImageColumn::make('images')
|
||||
->collection('listing-images')
|
||||
->circular(),
|
||||
TextColumn::make('id')->sortable(),
|
||||
TextColumn::make('title')->searchable()->sortable()->limit(40),
|
||||
TextColumn::make('category.name')->label('Category')->sortable(),
|
||||
TextColumn::make('user.email')->label('Owner')->searchable()->toggleable()->sortable(),
|
||||
TextColumn::make('price')
|
||||
->currency(fn (Listing $record): string => $record->currency ?: ListingPanelHelper::defaultCurrency())
|
||||
->sortable(),
|
||||
StateFusionSelectColumn::make('status')->sortable(),
|
||||
IconColumn::make('is_featured')->boolean()->label('Featured')->sortable(),
|
||||
TextColumn::make('city')->sortable(),
|
||||
TextColumn::make('country')->sortable(),
|
||||
TextColumn::make('created_at')->dateTime()->sortable(),
|
||||
])
|
||||
->filters([
|
||||
StateFusionSelectFilter::make('status'),
|
||||
SelectFilter::make('category_id')
|
||||
->label('Category')
|
||||
->relationship('category', 'name')
|
||||
->searchable()
|
||||
->preload(),
|
||||
SelectFilter::make('user_id')
|
||||
->label('Owner')
|
||||
->relationship('user', 'email')
|
||||
->searchable()
|
||||
->preload(),
|
||||
SelectFilter::make('country')
|
||||
->options(fn (): array => Country::nameOptions())
|
||||
->searchable(),
|
||||
SelectFilter::make('city')
|
||||
->options(fn (): array => City::nameOptions(null, false))
|
||||
->searchable(),
|
||||
TernaryFilter::make('is_featured')->label('Featured'),
|
||||
Filter::make('created_at')
|
||||
->label('Created Date')
|
||||
->schema([
|
||||
DatePicker::make('from')->label('From'),
|
||||
DatePicker::make('until')->label('Until'),
|
||||
])
|
||||
->query(fn (Builder $query, array $data): Builder => $query
|
||||
->when($data['from'] ?? null, fn (Builder $builder, string $date): Builder => $builder->whereDate('created_at', '>=', $date))
|
||||
->when($data['until'] ?? null, fn (Builder $builder, string $date): Builder => $builder->whereDate('created_at', '<=', $date))),
|
||||
Filter::make('price')
|
||||
->label('Price Range')
|
||||
->schema([
|
||||
TextInput::make('min')->numeric()->label('Min'),
|
||||
TextInput::make('max')->numeric()->label('Max'),
|
||||
])
|
||||
->query(fn (Builder $query, array $data): Builder => $query
|
||||
->when($data['min'] ?? null, fn (Builder $builder, string $amount): Builder => $builder->where('price', '>=', (float) $amount))
|
||||
->when($data['max'] ?? null, fn (Builder $builder, string $amount): Builder => $builder->where('price', '<=', (float) $amount))),
|
||||
])
|
||||
->filtersLayout(FiltersLayout::AboveContentCollapsible)
|
||||
->filtersFormColumns(3)
|
||||
->filtersFormWidth('7xl')
|
||||
->persistFiltersInSession()
|
||||
->defaultSort('id', 'desc')
|
||||
->actions(ResourceTableActions::editActivityDelete($resourceClass));
|
||||
}
|
||||
}
|
||||
@ -4,8 +4,8 @@ namespace Modules\Listing\Support;
|
||||
|
||||
use Filament\Forms\Components\DatePicker;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Textarea;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Components\Toggle;
|
||||
use Filament\Schemas\Components\Component;
|
||||
use Illuminate\Support\Carbon;
|
||||
@ -22,9 +22,6 @@ class ListingCustomFieldSchemaBuilder
|
||||
->exists();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, Component>
|
||||
*/
|
||||
public static function formComponents(?int $categoryId): array
|
||||
{
|
||||
return ListingCustomField::query()
|
||||
@ -38,10 +35,6 @@ class ListingCustomFieldSchemaBuilder
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $values
|
||||
* @return array<int, array{label: string, value: string}>
|
||||
*/
|
||||
public static function presentableValues(?int $categoryId, array $values): array
|
||||
{
|
||||
if ($values === []) {
|
||||
@ -66,12 +59,12 @@ class ListingCustomFieldSchemaBuilder
|
||||
$label = $field?->label ?: Str::headline((string) $key);
|
||||
|
||||
if (is_bool($value)) {
|
||||
$displayValue = $value ? 'Evet' : 'Hayır';
|
||||
$displayValue = $value ? 'Yes' : 'No';
|
||||
} elseif (is_array($value)) {
|
||||
$displayValue = implode(', ', array_map(fn ($item): string => (string) $item, $value));
|
||||
} elseif ($field?->type === ListingCustomField::TYPE_DATE) {
|
||||
try {
|
||||
$displayValue = Carbon::parse((string) $value)->format('d.m.Y');
|
||||
$displayValue = Carbon::parse((string) $value)->format('M j, Y');
|
||||
} catch (\Throwable) {
|
||||
$displayValue = (string) $value;
|
||||
}
|
||||
|
||||
63
Modules/Listing/Support/ListingImageViewData.php
Normal file
63
Modules/Listing/Support/ListingImageViewData.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Listing\Support;
|
||||
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
||||
|
||||
final class ListingImageViewData
|
||||
{
|
||||
private const CONTEXTS = [
|
||||
'card' => ['mobile' => 'card-mobile', 'desktop' => 'card-desktop'],
|
||||
'gallery' => ['mobile' => 'gallery-mobile', 'desktop' => 'gallery-desktop'],
|
||||
'thumb' => ['mobile' => 'thumb-mobile', 'desktop' => 'thumb-desktop'],
|
||||
];
|
||||
|
||||
public static function fromMedia(Media $media, string $context = 'card'): array
|
||||
{
|
||||
$conversion = self::CONTEXTS[$context] ?? self::CONTEXTS['card'];
|
||||
|
||||
return [
|
||||
'mobile' => $media->getAvailableUrl([$conversion['mobile'], $conversion['desktop']]),
|
||||
'desktop' => $media->getAvailableUrl([$conversion['desktop'], $conversion['mobile']]),
|
||||
'fallback' => $media->getUrl(),
|
||||
'alt' => trim((string) $media->name),
|
||||
];
|
||||
}
|
||||
|
||||
public static function fromUrl(?string $url): ?array
|
||||
{
|
||||
$value = is_string($url) ? trim($url) : '';
|
||||
|
||||
if ($value === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
'mobile' => $value,
|
||||
'desktop' => $value,
|
||||
'fallback' => $value,
|
||||
'alt' => '',
|
||||
];
|
||||
}
|
||||
|
||||
public static function pickUrl(?array $image, string $viewport = 'desktop'): ?string
|
||||
{
|
||||
if (! is_array($image)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$preferred = $viewport === 'mobile'
|
||||
? ($image['mobile'] ?? null)
|
||||
: ($image['desktop'] ?? null);
|
||||
|
||||
if (is_string($preferred) && trim($preferred) !== '') {
|
||||
return trim($preferred);
|
||||
}
|
||||
|
||||
$fallback = $image['fallback'] ?? null;
|
||||
|
||||
return is_string($fallback) && trim($fallback) !== ''
|
||||
? trim($fallback)
|
||||
: null;
|
||||
}
|
||||
}
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
namespace Modules\Listing\Support;
|
||||
|
||||
use App\Settings\GeneralSettings;
|
||||
use Modules\Site\App\Settings\GeneralSettings;
|
||||
use Throwable;
|
||||
|
||||
class ListingPanelHelper
|
||||
@ -32,7 +32,7 @@ class ListingPanelHelper
|
||||
return self::currencyCodes()[0] ?? 'USD';
|
||||
}
|
||||
|
||||
public static function normalizeCurrency(null | string $currency): string
|
||||
public static function normalizeCurrency(?string $currency): string
|
||||
{
|
||||
$normalized = strtoupper(substr(trim((string) $currency), 0, 3));
|
||||
$codes = self::currencyCodes();
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user