Neeraj1005's avatar

SQLSTATE[23000]: Integrity constraint violation: 4025 CONSTRAINT `media.manipulations` failed for `supportcrm`.`media` (SQL: insert into `media` (`updated_at`, `created_at`) values (2020-02-22 15:22:19, 2020-02-22 15:22:19))

I have used the media library package. when I tried to store an image it does not storing the image can anyone tell how to store images? this is my store function for medialibrary

    public function store(Request $request)
    {
       $avatar = new Media();

        $avatar->save();

        if($request->hasFile('image') && $request->file('image')->isValid()){
            $avatar->addMediaFromRequest('image')->toMediaCollection('media_lib');
        }
        // dd($avatar);
        return redirect()->route('gallary.index');

    }

This is my route

Route::resource('/gallary','MediaController')->middleware('auth');

This is my blade file

<form action="{{route('gallary.store')}}" method="post" enctype="multipart/form-data">
        @csrf
    <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
      <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <h5 class="modal-title" id="exampleModalCenterTitle">Add Media</h5>
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">&times;</span>
            </button>
          </div>
          <div class="modal-body">
            <div class="form-group">
              <label for="image">Upload Image</label>
              <input type="file" class="form-control-file" id="file" name="image">
              <label for="imageSize" style="color: #9e9b9b">Maximum upload file size: 2 MB</label>
            </div>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary">Submit</button>
          </div>
        </div>
      </div>
    </div>
</form>

And this is my modal

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;

class Media extends Model implements HasMedia
{
    use HasMediaTrait;

    
}

0 likes
4 replies
Tray2's avatar

You save the avatar before you assign any values to it.

Move this line

$avatar->save();

To just before your return statement.

Sinnbeck's avatar

I think you are misusing the package. It is made to add media to different models. Your model is the same name and would use the same table as the package

For instance you can add media to a an avatar model (personally I would probably add it to user). Look at their example where they add media to a news model (first page)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;

class Avatar extends Model implements HasMedia
{
    use HasMediaTrait;

    
}

Also you try to save it without data?

$avatar->save(); //should be create, and have the data needed to actually create a model in the database
Neeraj1005's avatar

@sinnbeck basically I want to create a separate Media section where user can add media. And list all media files like wordpress.

Please or to participate in this conversation.