Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use spatie/laravel-medialibrary for all medias #97

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion app/Filament/Clusters/Products/Resources/ProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public static function form(Form $form): Form
->collection('product-images')
->multiple()
->maxFiles(5)
->reorderable()
->acceptedFileTypes(['image/jpeg'])
->hiddenLabel(),
])
->collapsible(),
Expand Down Expand Up @@ -178,7 +180,8 @@ public static function table(Table $table): Table
->columns([
Tables\Columns\SpatieMediaLibraryImageColumn::make('product-image')
->label('Image')
->collection('product-images'),
->collection('product-images')
->conversion('thumb'),

Tables\Columns\TextColumn::make('name')
->label('Name')
Expand Down
16 changes: 11 additions & 5 deletions app/Filament/Resources/Blog/LinkResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Infolists\Components\ColorEntry;
use Filament\Infolists\Components\ImageEntry;
use Filament\Infolists\Components\SpatieMediaLibraryImageEntry;
use Filament\Infolists\Components\TextEntry;
use Filament\Infolists\Infolist;
use Filament\Notifications\Notification;
Expand Down Expand Up @@ -49,8 +49,9 @@ public static function form(Form $form): Form
->required()
->maxLength(255)
->columnSpanFull(),
Forms\Components\FileUpload::make('image')
->image(),
Forms\Components\SpatieMediaLibraryFileUpload::make('image')
->collection('link-images')
->acceptedFileTypes(['image/jpeg']),
]);
}

Expand All @@ -66,7 +67,9 @@ public static function infolist(Infolist $infolist): Infolist
->label('URL')
->columnSpanFull()
->url(fn (Link $record): string => '#' . urlencode($record->url)),
ImageEntry::make('image'),
SpatieMediaLibraryImageEntry::make('image')
->collection('link-images')
->conversion('thumb'),
]);
}

Expand All @@ -75,7 +78,10 @@ public static function table(Table $table): Table
return $table
->columns([
Tables\Columns\Layout\Stack::make([
Tables\Columns\ImageColumn::make('image')
Tables\Columns\SpatieMediaLibraryImageColumn::make('image')
->label('Image')
->collection('link-images')
->conversion('thumb')
->height('100%')
->width('100%'),
Tables\Columns\Layout\Stack::make([
Expand Down
16 changes: 10 additions & 6 deletions app/Filament/Resources/Blog/PostResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ public static function form(Form $form): Form

Forms\Components\Section::make('Image')
->schema([
Forms\Components\FileUpload::make('image')
->image()
->hiddenLabel(),
Forms\Components\SpatieMediaLibraryFileUpload::make('image')
->collection('post-images')
->hiddenLabel()
->acceptedFileTypes(['image/jpeg']),
])
->collapsible(),
]);
Expand All @@ -90,8 +91,10 @@ public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\ImageColumn::make('image')
->label('Image'),
Tables\Columns\SpatieMediaLibraryImageColumn::make('image')
->label('Image')
->collection('post-images')
->conversion('thumb'),

Tables\Columns\TextColumn::make('title')
->searchable()
Expand Down Expand Up @@ -201,7 +204,8 @@ public static function infolist(Infolist $infolist): Infolist
Components\SpatieTagsEntry::make('tags'),
]),
]),
Components\ImageEntry::make('image')
Components\SpatieMediaLibraryImageEntry::make('image')
->collection('post-images')
->hiddenLabel()
->grow(false),
])->from('lg'),
Expand Down
21 changes: 20 additions & 1 deletion app/Models/Blog/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\Translatable\HasTranslations;

class Link extends Model
class Link extends Model implements HasMedia
{
use HasFactory;
use HasTranslations;
use InteractsWithMedia;

/** @var string[] */
public $translatable = [
Expand All @@ -18,4 +22,19 @@ class Link extends Model
];

protected $table = 'blog_links';

public function registerMediaCollections(): void
{
$this
->addMediaCollection('link-images')
->useDisk('link-images')
->acceptsMimeTypes(['image/jpeg'])
->singleFile()
->registerMediaConversions(function (Media $media) {
$this
->addMediaConversion('thumb')
->width(256)
->height(144);
});
}
}
21 changes: 20 additions & 1 deletion app/Models/Blog/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\Tags\HasTags;

class Post extends Model
class Post extends Model implements HasMedia
{
use HasFactory;
use HasTags;
use InteractsWithMedia;

/**
* @var string
Expand Down Expand Up @@ -43,4 +47,19 @@ public function comments(): MorphMany
{
return $this->morphMany(Comment::class, 'commentable');
}

public function registerMediaCollections(): void
{
$this
->addMediaCollection('post-images')
->useDisk('post-images')
->acceptsMimeTypes(['image/jpeg'])
->singleFile()
->registerMediaConversions(function (Media $media) {
$this
->addMediaConversion('thumb')
->width(40)
->height(40);
});
}
}
15 changes: 15 additions & 0 deletions app/Models/Shop/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

class Product extends Model implements HasMedia
{
Expand Down Expand Up @@ -49,4 +50,18 @@ public function comments(): MorphMany
{
return $this->morphMany(Comment::class, 'commentable');
}

public function registerMediaCollections(): void
{
$this
->addMediaCollection('product-images')
->useDisk('product-images')
->acceptsMimeTypes(['image/jpeg'])
->registerMediaConversions(function (Media $media) {
$this
->addMediaConversion('thumb')
->width(40)
->height(40);
});
}
}
21 changes: 21 additions & 0 deletions config/filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@
'visibility' => 'public',
],

'product-images' => [
'driver' => 'local',
'root' => storage_path('app/public/product-images'),
'url' => env('APP_URL') . '/storage/product-images',
'visibility' => 'public',
],

'link-images' => [
'driver' => 'local',
'root' => storage_path('app/public/link-images'),
'url' => env('APP_URL') . '/storage/link-images',
'visibility' => 'public',
],

'post-images' => [
'driver' => 'local',
'root' => storage_path('app/public/post-images'),
'url' => env('APP_URL') . '/storage/post-images',
'visibility' => 'public',
],

's3' => [
'driver' => 's3',
'key' => env('AWS_ACCESS_KEY_ID'),
Expand Down
3 changes: 3 additions & 0 deletions database/factories/AddressFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use App\Models\Address;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Address>
*/
class AddressFactory extends Factory
{
protected $model = Address::class;
Expand Down
3 changes: 3 additions & 0 deletions database/factories/Blog/AuthorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use App\Models\Blog\Author;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Blog\Author>
*/
class AuthorFactory extends Factory
{
/**
Expand Down
3 changes: 3 additions & 0 deletions database/factories/Blog/CategoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Blog\Category>
*/
class CategoryFactory extends Factory
{
/**
Expand Down
15 changes: 11 additions & 4 deletions database/factories/Blog/LinkFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Database\Factories\Blog;

use Database\Factories\Concerns\CanCreateImages;
use App\Models\Blog\Link;
use Database\Seeders\LocalImages;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
Expand All @@ -12,8 +12,6 @@
*/
class LinkFactory extends Factory
{
use CanCreateImages;

/**
* Define the model's default state.
*
Expand All @@ -34,7 +32,16 @@ public function definition(): array
'nl' => $this->faker->sentence(),
],
'color' => $this->faker->hexColor(),
'image' => $this->createImage(LocalImages::SIZE_1280x720),
];
}

public function configure(): LinkFactory
{
return $this->afterCreating(function (Link $product) {
$product
->addMedia(LocalImages::getRandomFile(LocalImages::SIZE_1280x720))
->preservingOriginal()
->toMediaCollection('link-images');
});
}
}
18 changes: 14 additions & 4 deletions database/factories/Blog/PostFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
namespace Database\Factories\Blog;

use App\Models\Blog\Post;
use Database\Factories\Concerns\CanCreateImages;
use Database\Seeders\LocalImages;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Blog\Post>
*/
class PostFactory extends Factory
{
use CanCreateImages;

/**
* @var string
*/
Expand All @@ -22,10 +23,19 @@ public function definition(): array
'title' => $title = $this->faker->unique()->sentence(4),
'slug' => Str::slug($title),
'content' => $this->faker->realText(),
'image' => $this->createImage(),
'published_at' => $this->faker->dateTimeBetween('-6 month', '+1 month'),
'created_at' => $this->faker->dateTimeBetween('-1 year', '-6 month'),
'updated_at' => $this->faker->dateTimeBetween('-5 month', 'now'),
];
}

public function configure(): PostFactory
{
return $this->afterCreating(function (Post $product) {
$product
->addMedia(LocalImages::getRandomFile(LocalImages::SIZE_200x200))
->preservingOriginal()
->toMediaCollection('post-images');
});
}
}
3 changes: 3 additions & 0 deletions database/factories/CommentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use App\Models\Comment;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Comment>
*/
class CommentFactory extends Factory
{
/**
Expand Down
23 changes: 0 additions & 23 deletions database/factories/Concerns/CanCreateImages.php

This file was deleted.

3 changes: 3 additions & 0 deletions database/factories/Shop/BrandFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Shop\Brand>
*/
class BrandFactory extends Factory
{
/**
Expand Down
3 changes: 3 additions & 0 deletions database/factories/Shop/CategoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Shop\Category>
*/
class CategoryFactory extends Factory
{
/**
Expand Down
Loading