Skip to main content

Sync Filters

Sync filters determine which records from your models are synchronized to each user. Proper filtering is essential for security, performance, and ensuring users only receive relevant data.

Basic Filtering

User-Owned Records

The most common filter restricts records to those owned by the current user. This is a good place to start when determining what is relevant to sync:

Public Records Only

For models with public/private visibility:

No Filter (All Records)

If all authenticated users should see all records:
Security Risk: Only omit sync filters for truly public data that all authenticated users should access. Always default to restrictive filtering.

Advanced Filtering Patterns

Team-Based Access

Filter records based on team membership:

Role-Based Filtering

Different records based on user roles:

Multi-Tenant Filtering

Tenant Isolation

Ensure complete data isolation between tenants:

Hierarchical Tenants

For complex tenant hierarchies:

Dynamic Filtering

Time-Based Filtering

Filter records based on temporal constraints:

Performance Optimization

Database Indexes

Ensure your filters use proper database indexes:

Efficient Query Patterns

Write filters that utilize indexes effectively:

Eager Loading Relationships

Prevent N+1 queries in relationship-based filters:

Security Considerations

Input Validation

Always validate user inputs used in filters:

Authorization Checks

Combine filters with explicit authorization:

Troubleshooting

Problem: Users receive no synchronized records.Debugging:
  1. Check if syncFilter() method exists and returns valid query
  2. Verify filter conditions match actual data
  3. Test filter query directly: Model::query()->syncFilter()->toSql()
  4. Check authentication state in filter: auth()->check()
Problem: Users receive unauthorized data.Solutions:
  1. Review filter logic for security holes
  2. Add explicit null checks for auth()->user()
  3. Test with different user roles and permissions
  4. Use whereRaw('1 = 0') as a safe default for unauthorized access
  5. Add a policy to your model that checks whether the user can view this resource.
Problem: Sync operations are slow.Solutions:
  1. Add database indexes for filter columns
  2. Avoid complex joins in filters
  3. Use explain to analyze query performance
  4. Consider caching expensive relationship queries

Next Steps

Write Actions

Set up server-side write operations with validation

React Integration

Use filtered data in your React components

You now understand how to implement secure and efficient sync filters. Continue with React Integration to learn how to use your synchronized data in the frontend.