PHP code example of elseoclub / modelfiler

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

    

elseoclub / modelfiler example snippets




namespace App\Models;

use Elseoclub\Modelfiler\Traits\WithModelFiler;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use WithModelFiler;

    protected bool $acceptAnyFile = true; // To accept any file type

    protected static array $fileTypes = [
        'photo' => [
            'accept'   => '.jpg,.png,.jpeg,.gif,.svg',
            'storage'  => 'public',
            'max_size' => 200048, // Size in kilobytes
            'unique'   => true,
            'path'     => 'photos', // Define the folder path for storage
        ],
        'pictures' => [
            'accept'   => '.jpg,.png,.jpeg,.gif,.svg',
            'storage'  => 'public',
            'max_size' => 1000000, // Size in kilobytes
            'unique'   => false,
            'path'     => 'pictures', // Define the folder path for storage
        ],
    ];
}

$user->addFile($file, 'pictures'); // Add a file to the user

    $user->files;
    

    $user->files('pictures')->get();
    

    $user->files('pictures')->paginate(10);
    

    $user->deleteFile($file);
    

$accept = User::fileAccept('pictures'); // Get accepted MIME types

$rules['file'] = User::fileRule('pictures', true); // where true is 
bash
php artisan migrate