1

so i am trying to override laravel-medialibrary so that when i run

$modelA
  ->addMedia($attachment)
  ->withCustomProperties(['model_id' => $modelA->id])
  ->toMediaCollection('attachments_a');

, it adds a record into another specified database eg: database1_mysql

However, it is now currently adding into my current database(database2) instead..

I have created a Media model which has a connection already to another database

namespace App\Domains\Test\Models\Media;
class Media extends Model
{    
   protected $connection = 'database1_mysql'; 
}

And the model im relating the media to, i also have overriden the relationship to refer to the media that is connected to another table.

use App\Domains\Test\Models\Media;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

class ModelA extends Model implements HasMedia
{

 use InteractsWithMedia;

    public function media(): MorphMany
    {
        return $this->morphMany(Media::class, 'model');
    }
}
apokryfos
  • 38,771
  • 9
  • 70
  • 114
Napmi
  • 521
  • 2
  • 13
  • 32

1 Answers1

1

found a way. https://spatie.be/docs/laravel-medialibrary/v8/advanced-usage/using-your-own-model#breadcrumb

just swap the model in config

Napmi
  • 521
  • 2
  • 13
  • 32