Laravel open admin - how I can display media link using spatie media library

51 Views Asked by At

I have a laravel project based on open admin. I'm stuck with displaying model details. For media handling I use the media spatie library. My code: controller

protected function detail($id)
{
    $show = new Show(Document::findOrFail($id));
    $show->field('id', __('Id'));  
    $show->field('client', __('Client'));
    $show->field('description', __('Description'));
    $show->field('created_at', __('Created at'));
    

    $show->media('Attachment details', function ($media) {
        $media->mime_type();
        $media->file_name()->link($href = ''), $target = '_blank');

    });

    return $show;
}

Model file:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

class Document extends Model implements HasMedia
{
    use InteractsWithMedia, SoftDeletes;
    
    protected $fillable = [
        'client',
        'description',
        'created_at',
        'updated_at',
    ];
    
    
    public function getDocumentFileAttribute()
    {
        return $this->getMedia('document_file')->last();
    }
    
    public function registerMediaConversions(Media $media = null): void
    {
        $this->addMediaConversion('thumb')->width(50)->height(50);
    }
    
  }

I don't know how to generate the file URL and display it properly in the code. When I try to substitute the $link variable example:

$link = '/storage/'.$media->file_name();

I get an error:

Object of class OpenAdmin\Admin\Grid\Column could not be converted to string

0

There are 0 best solutions below