I update the picture with the spatie media library and my picture does not appear

my controller:

    public function update(SettingUpdateRequest $request, Setting $id){

    $setting = Setting::first();

   $setting->update($request->post());

    if ($request->hasFile('site_logo')) {
        $setting->addMedia($request->site_logo)->toMediaCollection('document');
    }
    return redirect()
        ->route('back.setting.index')->withMessage('Ayarlar başarıyla güncellendi!');

}

my model:

    namespace AppModels;

   use IlluminateDatabaseEloquentFactoriesHasFactory;
   use IlluminateDatabaseEloquentModel;
   use SpatieMediaLibraryHasMedia;
   use SpatieMediaLibraryInteractsWithMedia;

class Setting extends Model implements HasMedia
{
use HasFactory , InteractsWithMedia;
protected $table='settings';
protected $fillable=['site_title','site_desc','site_email'];
protected $appends = ['site_logo'];


public function getSiteLogoAttribute()
{
    return $this->getMedia('document')->last();
}
}

my picture way in stroge:
https://prnt.sc/11q22mc

my blade:

  <div class="form-group">
   <label for="exampleInputFile">Site Logo</label>
   <input type="file" id="exampleInputFile" name="site_logo" class="form-control" accept="image/*">
   </div>
   <label><strong>Resimler:</strong></label>
    @if (!$setting->site_logo)
    <span class="form-text text-muted">Bu Rehber ile ilgili resim bulunamadı. Yeni bir resim ekleyin! 
    </span>
    @endif
   <div class="col-lg-3">
    <div class="image-input image-input-empty image-input-outline" id="kt_image_5" style="background-image: url({{$setting->getFirstMediaUrl('document')}})">
    <div class="image-input-wrapper"></div>

    <span class="btn btn-xs btn-icon btn-circle btn-white btn-hover-text-primary btn-shadow" data-action="cancel" data-toggle="tooltip" title="" data-original-title="Cancel avatar">
        <i class="ki ki-bold-close icon-xs text-muted"></i>
   </span>
       <span class="btn btn-xs btn-icon btn-circle btn-white btn-hover-text-primary btn-shadow" data-action="remove" data-toggle="tooltip" title="" data-original-title="Remove avatar">
     </span>
     </div>
   </div>

my database:
https://prnt.sc/11q2b2l

I update the picture with the spatie media library and my picture does not appear.

https://prnt.sc/11q2fvi
my console fault:
https://prnt.sc/11q2k8q

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You should retrieve the last media in your blade. As you’ve already defined a getter in your model, you can use below code.

{{ $setting->site_logo->getUrl() }}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x