PHP code example of gsmeira / laravel-attachments

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

    

gsmeira / laravel-attachments example snippets


$table->json('attachments')->nullable();

class MyModel extends Model
{
    use HasAttachments;
}

MyModel::create([
    // ...
    'attachments' => [
        'file' => $request->file,
    ],
]);

$myModel->update([
  // ...
  'attachments' => [
    'file' => $request->file,
  ],
]);

$myModel->update([
  'attachments' => [
    'file' => $request->file,
  ],
]);

$myModel->updateAttachments([
  'file' => $request->file,
]);

$myModel->update([
  'attachments' => [
    'file' => null,
  ],
]);

$myModel->deleteAttachment('file');

$myModel->deleteAttachment(['file', 'cover_image']);

$myModel->update([
  'attachments' => null,
]);

$myModel->delateAttachments();

public function attachmentsBaseFolder(): string
{
    return 'users';
}

public function isAttachmentsPathObfuscationEnabled(): bool
{
    return true;
}

public function attachmentsPathObfuscationLevels(): int
{
    return 2;
}

public function attachmentsDisk(): string
{
    return 'private';
}

protected function update(User $user, Request $request) {
    $request->validate([
        'file' => ['=> $request->file,
        ],
    ]);

    // ...
}

php artisan vendor:publish --tag=laravel-attachments