phplaravelpackagelaravel-novalaravel-medialibrary

Call to undefined method Whitecube\NovaFlexibleContent\Layouts\Layout::getMedia()


I'm a bit stuck wondering what I'm doing wrong. I'm trying to use the spatie/laravel-medialibrary & ebess/advanced-nova-media-library together with nova-flexible-content In nova it works wonderful but can't get the image show in my blade. If anybody has any suggestions or know what I'm doing wrong. Thanks in advance for your time :) I followed there guide too: https://whitecube.github.io/nova-flexible-content/#/?id=usage-with-ebessadvanced-nova-media-library

My layout looks as follows:

<?php

namespace App\Nova\Flexible\Layouts;

use Whitecube\NovaFlexibleContent\Layouts\Layout;
use Spatie\MediaLibrary\HasMedia;
use Ebess\AdvancedNovaMediaLibrary\Fields\Images;
use Whitecube\NovaFlexibleContent\Concerns\HasMediaLibrary;

class Image extends Layout implements HasMedia
{
    use HasMediaLibrary;

    protected $name = 'image';

    protected $title = 'Image';

    public function fields()
    {
        return [
            Images::make('Image', 'images'),
        ];
    }
}

And my model:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Whitecube\NovaFlexibleContent\Concerns\HasFlexible;

class Job extends Model implements HasMedia
{
    use HasFactory;
    use HasFlexible;
    use InteractsWithMedia;

    protected $guareded = [];

    protected $casts = [
        'publish_date' => 'date',
    ];
}

Controller:

public function show(Job $job)
    {
        return view('jobs.show', compact('job'));
    }

Blade:

@foreach ($job->flexible('content') as $block)
        {{ $block->getMedia('images') }}
    @endforeach

composer:

"ebess/advanced-nova-media-library": "^4.0",
"spatie/laravel-medialibrary": "^10.0.0",
"whitecube/nova-flexible-content": "^1.0"

Solution

  • Following answer also can be found in the related Github issue.

    I just solved my problem and it was that I'm casting the wrong class.

    Let me explain a little bit more:

    Before

    protected $casts = [
        'layouts' => \Whitecube\NovaFlexibleContent\Value\FlexibleCast::class
    ];
    

    After

    protected $casts = [
        'layouts' => \App\Casts\MyFlexibleCast::class
    ];
    

    Conclusion

    If you're using custom layout classes like me, you need to cast the right class.