mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/compatibility/mongodb-laravel-integration
MIT License
6.99k stars 1.42k forks source link

GridFS #262

Closed jrvaja closed 6 years ago

jrvaja commented 10 years ago

First of all, I would like to say thanks for such brilliant Lib for Laravel. I have used this API in my first MongoDB project and it works like charm. I request you to add feature of GridFD compatibility in this API. I will be highly appreciate , if you can further assist me in this regards.

Cheers

mikebronner commented 10 years ago

Duplicate request -- see #231 which does recommend a solution/workaround further down in the comments. Still would be nice to see this added to the Moloquent core.

jenssegers commented 10 years ago

GridFS falls a bit outside of the Eloquent scope. If you want to use GridFS you can get it like this:

$grid = DB::getGridFS();
mikebronner commented 10 years ago

I was thinking perhaps a GridFS field type, but that might be extrapolating too much. Something where you could save a file to a model, and it would just store the GridFS File ID in the file, and save it to GridFS behind the scenes, and the same with retrieval. But that might be convoluting things, instead of making it easier.

jenssegers commented 10 years ago

Could you show me how you would prefer to use it in combination with the a model class?

mikebronner commented 10 years ago

Just off the top of my head, something like this:

class Book extends Moloquent
{
    protected $fillable = [
        'title',
        'coverArt',
        'author'
    ];

    protected $gridFs = [
        'coverArt'
    ];
}

Then you could do something like this:

$book = new Book();
$book->coverArt = Input::file('image');
$book->save();

This would then save the image to gridfs, save the fileid to the book. When referencing gridfs elements in models, it would do the opposite: using the fileid stored in the book, it would retrieve and return the file from gridfs.

Storing of the file to gridfs would only work on save methods (create, save, update, etc.), and deleting it would happen on update, save, delete, etc.).

This is just an idea of how it might be incorporated, food for thought if you find this intriguing. :) I realize that as it stands there may be issues deciding when a file needs to be deleted from GridFS and preventing orphaned files left and right.

aleksai commented 8 years ago

Hello, @jenssegers

Not sure if it's issue of this package, but I'm craving for your help. I'm using multiple db connections and the main is mysql, so I'm trying to get GridFS like this:

$grid = DB::connection('mongo')->getMongoDB()->getGridFS();

But it's throwing "Call to undefined method MongoDB\Database::getGridFS()". I did read #838, but I've checked phpinfo and pecl drivers mongo and mongodb are up to date.

So I'm stuck. Where should I dig now?

lindelius commented 8 years ago

Note: The getGridFS() function is no longer available in the new PHP MongoDB driver, which means that the code snippets above does not work if you have migrated to the new driver. In order to use GridFS with the new driver you will either have to import and use the MongoDB\GridFS classes from the official PHP library, or use the PHP driver classes directly.

JoeyHoutenbos commented 7 years ago

It is possible: https://github.com/jenssegers/laravel-mongodb/issues/826#issuecomment-290362789

bisht2050 commented 2 months ago

GridFS is now supported! Follow the docs to learn how to use GridFS : https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/filesystems/