spatie / laravel-medialibrary

Associate files with Eloquent models
https://spatie.be/docs/laravel-medialibrary
MIT License
5.71k stars 1.07k forks source link

Question: Hi can we use this library along with laravel mongodb package? #37

Closed atapatel closed 9 years ago

atapatel commented 9 years ago

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

freekmurze commented 9 years ago

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

spawn-guy commented 9 years ago

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:

spawn-guy commented 9 years ago

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.

spawn-guy commented 9 years ago

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?

freekmurze commented 9 years ago

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

spawn-guy commented 9 years ago

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

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.

freekmurze commented 9 years ago

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.

vesper8 commented 6 years ago

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

vesper8 commented 6 years ago

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 Spatie\MediaLibrary\Media model.

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

tolgaCTRLF5 commented 6 years ago

@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!

ClaudioVarandas commented 6 years ago

@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

meiyerDev commented 3 years ago

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;

robertdrakedennis commented 3 years ago

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

Luigidefra commented 3 years ago

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;

does it work? i are using Laravel 8.0, jenssegers/mongodb 3.8 and spatie/laravel-medialibrary 7.19 because i work on php 7.3 and these steps don't work for me, i also replaceuse Illuminate\Database\Eloquent\Relations\MorphTo; with use Jenssegers\Mongodb\Relations\MorphTo; on step2