PHP code example of dizatech / attachment

1. Go to this page and download the library: Download dizatech/attachment 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/ */

    

dizatech / attachment example snippets




'disks' => [
        'private' => [
        'driver' => 'local',
        'root' => storage_path('app/private'),
        'visibility' => 'private'
        ],
],




/*
* Filesystems that can be used for media storage
*
* Uploader will throw an exception if a disk not in this list is selected
*/
'allowed_disks' => [
    'public',
    'private',
],



php artisan storage:link



php artisan migrate




// How to use a custom disk for upload, (e.g) a ftp disk or custom local disk:

// FTP DISK:

    // 1- create a disk in /config/filesystems.php

        'disk_name' => [
            'driver' => 'ftp',
            'host' => env('FTP1_HOST'),
            'username' => env('FTP1_USERNAME'),
            'password' => env('FTP1_PASSWORD'),
    
            // Optional FTP Settings...
            'port' => 21,
            'root' => '/public_html',
            'protocol' => 'http'
            // 'passive' => true,
            // 'ssl' => true,
            // 'timeout' => 30,
        ],

    // 2- add this lines to .env file
    
        FTP1_HOST=server1.domain.com
        FTP1_USERNAME=your_username
        FTP1_PASSWORD=your_password
    
    // 3- add this disk name to /config/mediable.php
        
        'allowed_disks' => [
            'public',
            'private',
            'disk_name'
        ],

    // 4- use in blade
        <html>
        <x-attachment type="image" 
                multiple="false" 
                page="create" 
                name="feature" 
                label="تصویر شاخص"
                disk="disk_name"
        ></x-attachment>
        </html>

// LOCAL DISK:

    // 1- create a disk in /config/filesystems.php

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

    // 2- add this disk name to /config/mediable.php

        'allowed_disks' => [
            'public',
            'private',
            'disk_name'
        ],

    // 3- use in blade
        <html>
        <x-attachment type="image" 
                multiple="false" 
                page="create" 
                name="feature" 
                label="تصویر شاخص"
                disk="disk_name"
        ></x-attachment>
        </html>

// Notice: We can't use private disks for (image and video) types, but can use for attachmanet type.




return [
    'image_valid_mimes' => 'jpeg,png,jpg,gif',
    'image_maximum_size' => 5, // Megabyte
    // The first variant of list is main variant and we use it for thumbnail,
    // and if we want to define a new variant read palnk/laravel-mediable document
    // and after define variant in Service Provider. add the variant name to this array.
    'image_variant_list' => ['thumbnail'], 

    'attachment_valid_mimes' => 'pdf,doc,docx,xls,xlsx,jpeg,jpg,png,bmp',
    'attachment_maximum_size' => 10, // Megabyte
    'attachment_download_link_expire_time' => 6, // Hours

    'mimes_validation_message' => 'بارگذاری این نوع فایل مجاز نمی‌باشد.',
    'size_validation_message' => 'اندازه فایل بیشتر از حد مجاز می‌باشد.',

    'remove_file_success_message' => 'فایل با موفقیت حذف شد.',
    'remove_file_failed_message' => 'فایل مورد نظر پیدا نشد.',

    'hash_file_names' => false,

    'set_middleware_to_upload_url' => ['web'],
    'set_middleware_to_remove_url' => ['web'],
];




// Use Mediable trait in you're model:

use Plank\Mediable\Mediable;
class Post extends Model
{
    use HasFactory, SoftDeletes, Mediable;
}




// Attach array of uploaded files to model:

public function store(Request $request, Post $post)
{
    // Save you're models data
    $post->fill($request->all());
    $post->save();

    // Attach feature image to saved post
    $feature_tag = 'feature';
    $post->syncMedia($request->feature, $feature_tag);
        
    // Attach gallery images to saved post
    $gallery_tag = 'galleries';
    $post->syncMedia($request->galleries, $gallery_tag);

    // Update Media model with feature image caption
    if($request->has('feature')) {
        foreach ($request->feature as $index => $feature) {
            Media::query()->whereId($feature)->update([
                'caption' => $request->feature_caption[$index] ?? null
            ]);
        }
    }

    // Update Media model with gallery images captions
    if($request->has('galleries')) {
        foreach ($request->galleries as $index => $gallery) {
            Media::query()->whereId($gallery)->update([
                'caption' => $request->galleries_caption[$index] ?? null
            ]);
        }
    }
}




$article->getMedia('featured_image')->count() > 0 ? $article->getMedia('featured_image')->first()->getUrl() : '';
// and for show variant
$article->getMedia('featured_image')->count() > 0 ? $article->getMedia('featured_image')->first()->findVariant('thumbnail')->getUrl() : '';
bash

php artisan vendor:publish --provider="Plank\Mediable\MediableServiceProvider"
php artisan vendor:publish --tag=dizatech_attachment