1. Resources

User

public function toArray(Request $request): array
{
    return [
        'id'    => $this->id,
        'name'  => $this->name,
        'email' => $this->email,
    ];
}

Merchant

public function toArray(Request $request): array
{
    return [
        'id'       => $this->id,
        'uid'      => $this->uid,
        'name'     => $this->name,
        'phone'    => $this->phone,
        'email'    => $this->email,
        'category' => $this->category,
        'has_notes' => $this->notes->count(),
        'created_at' => $this->created_at,
    ];
}

Note

public function toArray(Request $request): array
{
    return [
        'id'          => $this->id,
        'uid'         => $this->uid,
        'title'       => $this->title,
        'body'        => $this->body,
        'type'        => $this->type,
        'status'      => $this->status,
        
        // Include name only if the correspond relationship has been loaded
        'merchant'    => $this->whenLoaded('merchant', fn () => $this->merchant->name),
        'created_by'  => $this->whenLoaded('creator', fn () => $this->creator->name),
        'assigned_to' => $this->whenLoaded('assignee', fn () => $this->assignee->name),
        
        'created_at'  => $this->created_at->toFormattedDateString(),
        'updated_at'  => $this->updated_at->toFormattedDateString(),
    ];
}

2. Requests

Store (New) Note

// Check user is authorized to make this request
public function authorize(): bool
{
    return auth()->check();
}

// Get the validation rules that apply to the request
public function rules(): array
{
    return [
        'merchantId' => ['required', 'exists:merchants,id'],
        'title' => ['required', 'string', 'max:255'],
        'body' => ['required', 'string'],
        'type' => ['sometimes', Rule::in(array_column(NoteType::cases(), 'value'))],
        'status' => ['sometimes', Rule::in(array_column(NoteStatus::cases(), 'value'))],
        'assigned_to' => ['nullable', 'exists:users,id'],
    ];
}

Update Note

// Check user is authorized to make this request
public function authorize(): bool
{
    return auth()->check();
}

// Get the validation rules that apply to the request
public function rules(): array
{
    return [
        'title' => ['required', 'string', 'max:255'],
        'body' => ['required', 'string'],
        'type' => ['sometimes', Rule::in(array_column(NoteType::cases(), 'value'))],
        'status' => ['sometimes', Rule::in(array_column(NoteStatus::cases(), 'value'))],
        'assigned_to' => ['nullable', 'exists:users,id'],
    ];
}

Filter Note

// Check user is authorized to make this request
public function authorize(): bool
{
    return auth()->check();
}

// Get the validation rules that apply to the request
public function rules(): array
{
    return [
        'search' => ['nullable', 'string', 'max:255'],
        'type' => ['nullable', Rule::in(array_column(NoteType::cases(), 'value'))],
        'status' => ['nullable', Rule::in(array_column(NoteStatus::cases(), 'value'))],
        'date.start' => ['nullable', 'date'],
        'date.end' => ['nullable', 'date', 'after_or_equal:date.start'],
    ];
}

3. Policy

Note

/**
 * Determine whether the user can update the model.
 */
public function update(User $user, Note $note): bool
{
    return $note->created_by === $user->id;
}

/**
 * Determine whether the user can delete the model.
 */
public function delete(User $user, Note $note): bool
{
    return $note->created_by === $user->id;
}