PHP code example of christopheraseidl / laravel-auto-filer
1. Go to this page and download the library: Download christopheraseidl/laravel-auto-filer 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/ */
christopheraseidl / laravel-auto-filer example snippets
use christopheraseidl\AutoFiler\HasAutoFiles;
class Product extends Model
{
use HasAutoFiles;
protected $fillable = [
'images',
'current_report',
'description',
];
protected function casts(): array
{
return [
'images' => 'array',
];
}
// Option 1: Using a property
protected $file = [
'images' => 'images',
'current_report' => 'documents'
];
// Option 2: Using a method
public function files(): array
{
return [
'images' => 'images',
'current_report' => 'documents'
];
}
// Property for rich text fields
protected $richText = [
'description' => 'content',
];
// Or using a method
public function richTextFields(): array
{
return [
'description' => 'content',
];
}
}
// Creating a new product with file uploads
$product = new Product([
'name' => 'Awesome Product',
'images' => [
'uploads/temp/image1.jpg',
'uploads/temp/image2.jpg',
],
'current_report' => 'uploads/temp/current_report.pdf',
]);
$product->save();
// Files are now moved to:
// - products/1/images/image1.jpg (with thumbnail: image1-thumb.jpg if enabled)
// - products/1/images/image2.jpg (with thumbnail: image2-thumb.jpg if enabled)
// - products/1/documents/current_report.pdf
// Updating files
$product->images = ['uploads/temp/new-image.jpg'];
$product->save();
// Old images and their generated thumbnails are deleted,
// new one is moved to products/1/images/new-image.jpg
// and a new thumbnail is automatically created if enabled
// Deleting the model
$product->delete();
// All associated files and their thumbnails are automatically deleted