Requires PHP 8.0+
Require Laravel Eloquent Images using Composer:
composer require zing/laravel-eloquent-images
use Zing\LaravelEloquentImages\Tests\Models\Product;
use Zing\LaravelEloquentImages\Image;
$product = Product::query()->first();
// Add image(s) to model
$product->attachImage("https://github.com/zingimmick/laravel-eloquent-images/workflows/tests/badge.svg");
$product->attachImages([
"https://github.com/zingimmick/laravel-eloquent-images/workflows/tests/badge.svg",
Image::query()->first()
]);
// Remove image(s) from model
$product->detachImage("https://github.com/zingimmick/laravel-eloquent-images/workflows/tests/badge.svg");
$product->detachImages([
"https://github.com/zingimmick/laravel-eloquent-images/workflows/tests/badge.svg",
Image::query()->first()
]);
// Reset images of model
$product->syncImages([
"https://github.com/zingimmick/laravel-eloquent-images/workflows/tests/badge.svg",
Image::query()->first()
]);
// Get images of model
$product->images;
// Eager load images
$products = Product::query()->with('images')->withCount('images')->get();
$products->each(function (Product $product){
$product->images->dump();
$product->images_count;
});
Laravel Eloquent Images is an open-sourced software licensed under the MIT license.