PHP code example of givanov95 / laravel-attachments
1. Go to this page and download the library: Download givanov95/laravel-attachments library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Givanov95\LaravelAttachments\Concerns\HasImages;
use Givanov95\LaravelAttachments\Concerns\HasFiles;
class Product extends Model
{
use HasImages, HasFiles;
// optional — cache first image path on this column for fast listing queries
// optional — cache first image path on this column for fast listing queries
protected function profileImageColumn(): ?string
{
return 'image_path';
}
}
use Givanov95\LaravelAttachments\Services\UploadHelper;
public function store(StoreProductRequest $request)
{
$product = new Product($request->validated());
$product->save();
$product
->setImages(
UploadHelper::uploadMultipleImages($request->validated(), 'images', 'products'),
'default'
)
->setImages(
UploadHelper::uploadMultipleImages($request->validated(), 'size_chart', 'products/charts'),
'size_chart'
)
->setFiles(
UploadHelper::uploadMultipleFiles($request->validated(), 'specs', 'products/specs'),
'specs'
)
->save();
return back();
}
$product->images; // ordered by `order` ASC
$product->files;
$product->firstImage; // most-min order
$product->latestImage; // most-recent created
$product->getGroupedImages(['default', 'size_chart']);