Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('attachables', function (Blueprint $table) {
$table->string('collection')->nullable();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('attachables', function (Blueprint $table) {
$table->dropColumn('collection');
});
}
};
39 changes: 39 additions & 0 deletions docs/basic-usage/configuring-your-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ $myModel = ModelName::find($modelId);

// Link attachment to your model.
$myModel->attachments()->attach($attachment);
// Or, to add the attachment to a specific collection:
$myModel->attachments()->attach($attachment, ['collection' => 'collection_name_here']);
```

## Detach attachments
Expand All @@ -45,3 +47,40 @@ $myModel = ModelName::find($modelId);
// Detach attachment from your model.
$myModel->attachments()->detach($attachment);
```

## Retrieve attachments
You can retrieve the attachments for your model in the following way:

```php
// Retrieve all attachments
$model->attachments()->get();

// Retrieve attachments for a specific collection
$model->attachments()->wherePivot('collection', 'collection_name_here')->get();
```

To make querying attachments in a specific collection easier, you can add a separate relationship method to your model:

```php
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use VanOns\LaravelAttachmentLibrary\Concerns\HasAttachments;

class ModelName extends Model
{
use HasAttachments;

public function gallery(): MorphToMany
{
return $this->attachmentCollection('gallery');
}
}
```

This allows you to retrieve the attachments in the `gallery` collection like so:

```php
$model->gallery()->get();
```
5 changes: 5 additions & 0 deletions src/Concerns/HasAttachments.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@ public function attachments(): MorphToMany
{
return $this->morphToMany(Config::get('attachment-library.model', Attachment::class), 'attachable');
}

public function attachmentCollection(string $collection): MorphToMany
{
return $this->attachments()->wherePivot('collection', $collection);
}
}
2 changes: 1 addition & 1 deletion src/LaravelAttachmentLibraryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function configurePackage(Package $package): void
{
$package->name('laravel-attachment-library')
->hasConfigFile(['attachment-library', 'glide'])
->hasMigrations(['create_attachments_table', 'create_attachables_table'])
->hasMigrations(['create_attachments_table', 'create_attachables_table', 'add_collection_to_attachables_table'])
->runsMigrations()
->hasViews('laravel-attachment-library')
->hasViewComponent('laravel-attachment-library', Image::class)
Expand Down