- How do I install mongodb/laravel-mongodb in a Laravel 10.x project?
- Run `composer require mongodb/laravel-mongodb` in your project directory. Ensure you also install the MongoDB PHP driver via PECL (`pecl install mongodb`) and configure your `.env` file with MongoDB connection details (e.g., `MONGODB_CONNECTION=default`). The package extends Eloquent automatically, so no additional setup is required for basic usage.
- Does this package support Laravel 11 or 12? Will it work with future Laravel versions?
- The package officially targets Laravel 10.x, but it’s designed to be forward-compatible. Check the [GitHub releases](https://github.com/mongodb/laravel-mongodb/releases) or [JIRA](https://jira.mongodb.org/browse/PHPORM) for updates. For older Laravel versions (e.g., 8.x, 9.x), use the respective branches (e.g., `3.9`). MongoDB typically backports critical fixes.
- Can I use Eloquent relationships (e.g., hasMany, belongsTo) with MongoDB?
- Yes, the package preserves Eloquent’s relationship syntax, but MongoDB lacks native SQL joins. Use `$lookup` in aggregation pipelines or denormalize data for performance. For example, `hasMany` will work, but complex joins may require custom logic or manual denormalization in migrations.
- How do I handle soft deletes in MongoDB with this package?
- The `SoftDeletes` trait is deprecated in favor of MongoDB’s native `deleted_at` field. Use `use SoftDeletes;` in your model and ensure your schema includes the field. Existing soft-deleted records will be marked with the `deleted_at` timestamp, and queries automatically exclude them via `whereNull('deleted_at')`.
- What are the performance implications of using MongoDB with Laravel’s Eloquent?
- Aggregation pipelines (e.g., `$lookup`, `$facet`) can be slower than SQL joins. Benchmark complex queries against raw MongoDB drivers. Indexes must be manually created (e.g., `createIndex()`), and multi-document transactions introduce latency. For high-throughput apps, consider caching frequent queries or using MongoDB’s change streams for real-time updates.
- How do I migrate data from MySQL to MongoDB using this package?
- Use Laravel’s schema migrations with `Schema::table()` or custom scripts to transform relational data into MongoDB documents. Tools like `doctrine/dbal` can help export SQL data, but you’ll need to denormalize relationships (e.g., embed child records as arrays). Test migrations in a staging environment, as schema changes may require downtime.
- Are there security risks when using raw MongoDB queries in Laravel?
- Yes, raw queries (e.g., `$where` or `Builder::raw()`) bypass Eloquent’s query builder protections, exposing you to injection risks. Always sanitize inputs and use parameterized queries. Store MongoDB credentials in `.env` (never hardcoded) and enforce TLS for connections. For sensitive operations, prefer Eloquent’s built-in methods.
- How do I monitor MongoDB performance in a Laravel app?
- Integrate with Laravel Scout or custom tools like Prometheus to track query execution time, index usage, and connection metrics. Enable MongoDB’s profiling level (`db.setProfilingLevel()`) for slow queries. The package doesn’t include built-in monitoring, but you can extend it with events (e.g., `ModelCreating`) to log operations.
- What alternatives exist for MongoDB in Laravel if this package doesn’t fit my needs?
- Consider `jenssegers/mongodb` (legacy, less maintained) or raw MongoDB PHP driver for full control. For hybrid setups, `spatie/laravel-mongodb` offers a simpler API but lacks Eloquent integration. If you need SQL-like features, evaluate PostgreSQL with JSONB or MongoDB Atlas’s serverless instances for auto-scaling.
- How do I verify the integrity of the mongodb/laravel-mongodb package before installing?
- Download the [PHP team’s GPG key](https://pgp.mongodb.com/php-driver.asc) and verify release tags locally. Run `gpg --import php-driver.asc` and then `git show --show-signature <tag>` (e.g., `4.4.0`) in the package’s repository. While Composer doesn’t natively support signature verification, this ensures the release hasn’t been tampered with.