PHP code example of akatekno / attachable

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

    

akatekno / attachable example snippets




namespace App\Models;

use Akatekno\Attachable\Interfaces\AttachableOne;
use Akatekno\Attachable\Traits\AttachableOne as TraitsAttachableOne;

class User implements AttachableOne
{
    use TraitsAttachableOne;

    // ... the rest of the code
}



use App\Models\User;

class Controller
{
    public function index()
    {
        return User::first()->attachment;
    }
}



use App\Models\User;
use Illuminate\Support\Facades\Route;

Route::post('attach', function (Request $request) {
    $file = $request->file('file');

    $user = User::first();

    $user->attachments()->create([
        'name' => $file->getClientOriginalName(),
        'path' => $file->store(date('Y-m-d')),
        'mime_type' => $file->getClientMimeType(),
        'extension' => $file->getClientOriginalExtension(),
        'size' => $file->getSize(),
        'type' => null,
    ]);
});



namespace App\Models;

use Akatekno\Attachable\Interfaces\AttachableMany;
use Akatekno\Attachable\Traits\AttachableMany as TraitsAttachableMany;

class User implements AttachableMany
{
    use TraitsAttachableMany;

    // ... the rest of the code
}



use App\Models\User;

class Controller
{
    public function index()
    {
        return User::first()->attachments;
    }
}



use App\Models\User;
use Illuminate\Support\Facades\Route;

Route::post('attach', function (Request $request) {
    $files = $request->allFiles();

    $user = User::first();

    /** @var \Illuminate\Http\UploadedFile $file */
    foreach ($files as $file) {
        $user->attachments()->create([
            'name' => $file->getClientOriginalName(),
            'path' => $file->store(date('Y-m-d')),
            'mime_type' => $file->getClientMimeType(),
            'extension' => $file->getClientOriginalExtension(),
            'size' => $file->getSize(),
            'type' => null,
        ]);
    }
});



namespace App\Models;

use Akatekno\Attachable\Models\Attachment;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;

class Reservation extends Model
{
    // ...

    public function visit_request_letter(): MorphOne
    {
        return $this->morphOne(Attachment::class, 'attachable')
            ->where('type', 'visit-request-letter');
    }

    public function hotel_booking_invoice(): MorphOne
    {
        return $this->morphOne(Attachment::class, 'attachable')
            ->where('type', 'hotel-booking-invoice');
    }

    public function other_attachments(): MorphMany
    {
        return $this->morphMany(Attachment::class, 'attachable')
            ->where('type', 'other-attachment');
    }

    // ...
}



// Visit Request Letter
$reservation->visit_request_letter()->create([
    'name' => $file->getClientOriginalName(),
    'path' => $file->store(date('Y-m-d')),
    'mime_type' => $file->getClientMimeType(),
    'extension' => $file->getClientOriginalExtension(),
    'size' => $file->getSize(),
    'type' => 'visit-request-letter',
]);

// Hotel Booking Invoice
$reservation->hotel_booking_invoice()->create([
    'name' => $file->getClientOriginalName(),
    'path' => $file->store(date('Y-m-d')),
    'mime_type' => $file->getClientMimeType(),
    'extension' => $file->getClientOriginalExtension(),
    'size' => $file->getSize(),
    'type' => 'hotel-booking-invoice',
]);

// Other Attachments
$reservation->other_attachments()->create([
    'name' => $file->getClientOriginalName(),
    'path' => $file->store(date('Y-m-d')),
    'mime_type' => $file->getClientMimeType(),
    'extension' => $file->getClientOriginalExtension(),
    'size' => $file->getSize(),
    'type' => 'other-attachment',
]);