Skip to content

Commit a90bbaf

Browse files
Merge pull request #28 from VanOns/feature/add-collections
feat: add support for attachment collections on a model
2 parents f92554b + e99b868 commit a90bbaf

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('attachables', function (Blueprint $table) {
15+
$table->string('collection')->nullable();
16+
});
17+
}
18+
19+
/**
20+
* Reverse the migrations.
21+
*/
22+
public function down(): void
23+
{
24+
Schema::table('attachables', function (Blueprint $table) {
25+
$table->dropColumn('collection');
26+
});
27+
}
28+
};

docs/basic-usage/configuring-your-model.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ $myModel = ModelName::find($modelId);
2929

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

3436
## Detach attachments
@@ -45,3 +47,40 @@ $myModel = ModelName::find($modelId);
4547
// Detach attachment from your model.
4648
$myModel->attachments()->detach($attachment);
4749
```
50+
51+
## Retrieve attachments
52+
You can retrieve the attachments for your model in the following way:
53+
54+
```php
55+
// Retrieve all attachments
56+
$model->attachments()->get();
57+
58+
// Retrieve attachments for a specific collection
59+
$model->attachments()->wherePivot('collection', 'collection_name_here')->get();
60+
```
61+
62+
To make querying attachments in a specific collection easier, you can add a separate relationship method to your model:
63+
64+
```php
65+
namespace App\Models;
66+
67+
use Illuminate\Database\Eloquent\Model;
68+
use Illuminate\Database\Eloquent\Relations\MorphToMany;
69+
use VanOns\LaravelAttachmentLibrary\Concerns\HasAttachments;
70+
71+
class ModelName extends Model
72+
{
73+
use HasAttachments;
74+
75+
public function gallery(): MorphToMany
76+
{
77+
return $this->attachmentCollection('gallery');
78+
}
79+
}
80+
```
81+
82+
This allows you to retrieve the attachments in the `gallery` collection like so:
83+
84+
```php
85+
$model->gallery()->get();
86+
```

src/Concerns/HasAttachments.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ public function attachments(): MorphToMany
1212
{
1313
return $this->morphToMany(Config::get('attachment-library.model', Attachment::class), 'attachable');
1414
}
15+
16+
public function attachmentCollection(string $collection): MorphToMany
17+
{
18+
return $this->attachments()->wherePivot('collection', $collection);
19+
}
1520
}

src/LaravelAttachmentLibraryServiceProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function configurePackage(Package $package): void
1717
{
1818
$package->name('laravel-attachment-library')
1919
->hasConfigFile(['attachment-library', 'glide'])
20-
->hasMigrations(['create_attachments_table', 'create_attachables_table'])
20+
->hasMigrations(['create_attachments_table', 'create_attachables_table', 'add_collection_to_attachables_table'])
2121
->runsMigrations()
2222
->hasViews('laravel-attachment-library')
2323
->hasViewComponent('laravel-attachment-library', Image::class)

0 commit comments

Comments
 (0)