Laravel-medialibrary: Question: Hi can we use this library along with laravel mongodb package?

Created on 4 Aug 2015  路  13Comments  路  Source: spatie/laravel-medialibrary

Hi Can we use this library along with laravel mongodb package? For Mongodb Operation we used this package library https://github.com/jenssegers/laravel-mongodb

question

Most helpful comment

@vesper8 I used your first step. But I kept getting PDO error. Adding below code did not help me,

protected $connection = 'mysql';

If anyone gets PDO error while saving a media library image you can try but if the source file gets updated you may come across with other problems:

1) The composer.json after a fresh install has a part that looks exactly like this
... "psr-4": { "App\\": "app/" } ...
Just change it to this
... "psr-4": { "App\\": "app/", "Spatie\\MediaLibrary\\": "fixes/Spatie/MediaLibrary/" } ...

2) Open the file /vendor/spatie/laravel-medialibrary/src/Models/Media.php and copy contents,

3) Create folders and file /fixes/Spatie/MediaLibrary/Models/Media.php and paste in the code.

4) Find

... class Media extends Model ...

Replace with

... class Media extends \Jenssegers\Mongodb\Eloquent\Model ...

5) composer dump-autoload

6) Profit!

All 13 comments

I haven't used that package myself, but I guess it'll work. Try it out, and let me know.

I will use this package with MongoDB today.
So far, from looking through the code of it i don't like the 2 mongo-specific things:

  • $casts to Array - is not needed for this db, as i'd prefer to save manipulations/etc as json-object, and not json-serialized string.
  • provided migration doesn't have any indexes. "Moloquent" ignores the fields, but respects Indexes. As only "indexes" exist in schema-less db. (migration went fine, btw. i see a collection, but no indexes, as expected)

For now i need to override the Media model with different Model class, Jenssegers\Mongodb\Model not the Illuminate one. I get Grammar errors (this is covered in docs of laravel-mongo)

also PK is _id not id.

i'll keep posting the results.

okay, i need help.
i'm lost trying to override the model with another model... euh, Models
what i want to do is
$app->when(Spatie\MediaLibrary\Media::class)->needs(Illuminate\Database\Eloquent\Model::class)->give(Jenssegers\Mongodb\Model::class);
but this doesn't work.

$app->when(Spatie\MediaLibrary\MediaRepository::class)->needs(Spatie\MediaLibrary\Media::class)->give(App\Media::class);
this does work, but. it is still derived from Illuminate\Model. if i copy the whole model contents (even leaving Illuminate\Model) - typeHinting breaks with
Argument 1 passed to Spatie\MediaLibrary\MediaRepository::__construct() must be an instance of Spatie\MediaLibrary\Media, instance of App\Media given

any suggestions?

Unfortunately no, I haven't got any experience with Mongo. I hope another user of the package can help you out.

i've got one level Up(not deeper) and trying to inject a Repository, that will use a proper model.

but now, everything is type-hinted,.. so to just change MediaModel to be MongoMediaModel i need to overwrite, like, everything.. which is not that good :/

i suppose a better approach would be a MediaModelInterface, that will be type-hinted everywhere in the package.
and then i'd just need to

  • create a MediaModel extend Mongodb\Model implement MediaModelInterface
  • $app->singleton(MediaModelInterface, App\Media)... or something

i _guess_ it could be done like that.

but due to lack of development time - i think i will not do that, at least for now. and will go a simplistic way with directly using filesystem

also i've noticed several $model->id usages which will definitely break with monogdb._id. but one can use Eloquent's ->getKeyName() which will give you a proper primary key name.

I'm going to close this for now. If you ever find a good way to solve these issues or find another way to integrate mongo, feel free to reopen this issue.

I know it's been 2 years but @spawn-guy did you have any luck ever getting this to work?

I want to assign media to a mongo model as well and running into problems

I managed to get it to work. Here's what did it:

First, in CreateMediaTable migration

replace

$table->morphs('model');

by

$table->string('model_id');
$table->string('model_type');
$table->index(["model_id", "model_type"], null);

Then prepare your MongoDb model like normally.. implements HasMedia and add HasMediaTrait

And that's pretty much it! Except.. I was having this suuuuper annoying problem which took me 2 hours to figure out but in the end the fix was so simple. I kept getting a PDO error.. what fixed it was adding

protected $connection = 'mysql';

To the SpatieMediaLibraryMedia model.

I guess, without this specification it would try to look for the Media on mongodb?

@vesper8 I used your first step. But I kept getting PDO error. Adding below code did not help me,

protected $connection = 'mysql';

If anyone gets PDO error while saving a media library image you can try but if the source file gets updated you may come across with other problems:

1) The composer.json after a fresh install has a part that looks exactly like this
... "psr-4": { "App\\": "app/" } ...
Just change it to this
... "psr-4": { "App\\": "app/", "Spatie\\MediaLibrary\\": "fixes/Spatie/MediaLibrary/" } ...

2) Open the file /vendor/spatie/laravel-medialibrary/src/Models/Media.php and copy contents,

3) Create folders and file /fixes/Spatie/MediaLibrary/Models/Media.php and paste in the code.

4) Find

... class Media extends Model ...

Replace with

... class Media extends \Jenssegers\Mongodb\Eloquent\Model ...

5) composer dump-autoload

6) Profit!

@tolgaCTRLF5 thx! works perfectly. :)

@freekmurze Is there a way to override a vendor / package Model ? like we do with the migrations/views .... ? I guess not but maybe you can have an idea.

Thanks
CV

Hello, I followed these steps

Step 1:
Copy this Model \Spatie\MediaLibrary\MediaCollections\Models\Media and paste in your custom model \App\Models\Spatie\MediaLibrary\Media

Step 2:
Find the use Illuminate\Database\Eloquent\Model; and replace with use Jenssegers\Mongodb\Eloquent\Model;

Step 3:
In the App\Providers\AppServiceProvider add this:

$loader = AliasLoader::getInstance();
$loader->alias(\Spatie\MediaLibrary\MediaCollections\Models\Media::class,\App\Models\Spatie\MediaLibrary\Media::class);

Don't forget import this class: use Illuminate\Foundation\AliasLoader;

@themey99 what version are you using? and is that all you added?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

xron89 picture xron89  路  3Comments

ideadx picture ideadx  路  4Comments

drtheuns picture drtheuns  路  3Comments

eichgi picture eichgi  路  3Comments

mohammad6006 picture mohammad6006  路  4Comments