PHP code example of akanaan / model-files

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

    

akanaan / model-files example snippets




namespace App\Models;

use AKanaan\ModelFiles\Traits\HasFiles;
use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    use HasFiles;

    protected $attaches = [
        'logo' => [
            'disk' => 'public',
            'path' => '/',
        ],
        'images' => [
            'disk' => 'public',
            'path' => 'images',
        ]
    ];
}


...
$product = Product::findOrFail($id);

$product->attachFile('logo', $request->file('logo'));
$product->attachFiles('images', [
    $request->file('images.0'),
    $request->file('images.1'),
    $request->file('images.2')
    ...
]);
...


...
$product = Product::findOrFail($id);

$product->detachFile('logo');
/*
 * Second param is optional (array of ids of files you want to delete)
 */
$product->detachFiles('images', [1, 2, 3]);
...


...
$product = Product::findOrFail($id);

// returns instance of AKanaan\ModelFiles\Models\File
$product->retrieveFile('logo');
// returns collection of AKanaan\ModelFiles\Models\File
$product->retrieveFiles('images');
...


...
$product = Product::findOrFail($id);

$logo = $product->retrieveFile('logo');
$publicUrl = $logo->url();
...
bash
php artisan migrate