RaymondE's avatar

Spati MediaLibrary not saving images

Hello,

i'm quite new to laravel and have an issue where i'm kinda stuck.

I'm using the MediaLibrary package from spatie and i'm trying to store an image that has beeen uploaded.

Everything works fine, expect that the image is not stored and no recored is placed in the database.

Maybe someone can help me out.

Thanks!

MediaController

<?php

namespace App\Http\Controllers;


use App\Media;
use Illuminate\Http\Request;

class MediaController extends Controller
{

    // Hintergrund Polygon hinzufügen
    public function addBackgroundImage(Request $request)
    {
     
     // Media::create()
     //         ->addMediaFromRequest('background')
     //         ->toMediaCollection();


        $media = new Media;

        $media->addMediaFromRequest('background')
              ->toMediaCollection('backgrounds');

        return redirect('/settings')->with('success', 'Hintergrund Bild hinzugefügt');
    }

}

Model

<?php

namespace App;

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

class Media extends Model implements HasMedia
{
        use HasMediaTrait;

}

Thanks!

0 likes
5 replies
RaymondE's avatar

I think so, yes.

{!! Form::open(['action' => 'MediaController@addBackgroundImage', 'method' => 'POST', 'files' => true]) !!}
    
      <div class="file-field input-field">
      <div class="btn">
        <span>Background</span>
        {{Form::input('file', 'background')}}
      </div>
      <div class="file-path-wrapper">
        <input class="file-path validate" type="text">
      </div>
    </div>

    <button class="btn waves-effect waves-light" type="submit" >Speichern
        <i class="material-icons right">send</i>
    </button>

{!! Form::close() !!}
            
click's avatar
click
Best Answer
Level 35

you need to save your $media object.

$media->save()
2 likes
mcangueiro's avatar

I actually noticed now that you are not saving your model. @m-rk response should do the trick.

RaymondE's avatar

man these beginner mistakes are really annoying^^

Thanks guys, this was the part that was missing :)

1 like

Please or to participate in this conversation.