2
votes

I am trying to use $media->onDuplicateReplace(); in code:

public function update($id){

        $user = User::find($id);
        $user->name=request('name');
        $user->email=request('email');
        $user->password=request('password');
        $user->description=request('description');
        $user->country_id=request('country');
        $user->state_id=request('state');
        $user->city_id=request('city');
        $user->works_at=request('works_at');
        $user->studies_at=request('studies_at');


        $uid = $id;
        $img = Image::make(request('file'));

        $img->resize(800, null, function ($constraint) {
            $constraint->aspectRatio();
        });

        $img->crop(800,800);
        $img->save('uploads/users/images/'.$uid.'.jpg');

        $media = MediaUploader::import('uploads', 'users/images', $uid, 'jpg');
        $media->onDuplicateReplace();
        $user->attachMedia($media, 'image');

        $user->update();
        return redirect('/');
    }

but i dont work for me, keeping sending the error:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'uploads-users/images-1-jpg' for key 'media_disk_directory_filename_extension_unique' (SQL: insert into media (disk, directory, filename, extension, mime_type, aggregate_type, size, updated_at, created_at) values (uploads, users/images, 1, jpg, image/jpeg, image, 49344, 2017-12-24 10:10:07, 2017-12-24 10:10:07))

this thing dont work too:

'on_duplicate' => Plank\Mediable\MediaUploader::ON_DUPLICATE_REPLACE,

How can i just make media to replace the file if it already exist?

2

2 Answers

0
votes

I noticed such a problem when turned back to MySQL 5.5.*. syncMedia or attachMedia don't matter because media table kind of messed up before ever attaching to mediables. MySQL 5.6 may have such error eliminated as many other good changes, so I'd recommend to update to it. So then use syncMedia to attach media.

By the way if the cause seems different or you aren't able to update MySQL on server (as me) there is "dirty" workaround. Delete current media and create new, so no conflicts will appear:

if ($user->hasMedia('image')) $user->firstMedia('image')->delete();
$media = MediaUploader::import('uploads', 'users/images', $uid, 'jpg');
$media->onDuplicateReplace();
$user->attachMedia($media->upload(), 'image');
0
votes

Maybe you use a different db connection to the one specified in the .env file?

For me this was the case and the Mediable checked always in the wrong database. To fix that, i had to implement my own Media model, extending the default model to be able to define the connection in the Model itself like i do in my other Models.

Because in my case the connection will be specified on runtime (laravel console tool with connection details input).

use Plank\Mediable\Media as PlankMedia;

class Media extends PlankMedia
{
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        if(!empty(config('database.connections.destination'))){
            $this->setConnection('destination');
        }
    }
}

and in the mediable.php config file i specified my custom model:

'model' => App\Models\Media::class,