Attachments
Suggest EditFiles of various formats and extensions related to the recording are attachments
Attachments can be attached to any model via links, for this you need to add a trait:
namespace App;
use Illuminate\Database\Eloquent\Model;
use Orchid\Attachment\Attachable;
class Hostel extends Model
{
use Attachable;
//
}
After that we can add and receive its attachments, for example:
$item = Hostel::find(42);
$item->attachment()->get();
Upload example
In fact, you already have a route for downloading files (unless, of course, access to it is allowed)
An example of a controller method:
use Orchid\Attachment\File;
public function upload(Request $request)
{
$file = new File($request->file('photo'));
$attachment = $file->load();
return response()->json()
}
This will automatically upload your file to the default repository (public) and create an entry in the database.
$image = $item->attachment()->first();
// Get the URL of the file
$image->url();
Reload
Thanks to the hash, attachments are not downloaded again, instead a link is created in the database to the required physical file, allowing efficient use of resources. The file will be deleted only when all links are destroyed.
Remove
Attachments won’t be removed after model removal automatically. In case when your attachments can’t exist without a
model, you should remove them on model deleting events manually. If you delete a row from the attachments
table, the file won’t be deleted. To clear your attachments, you need to use delete() function on the
Attachment model. In that case, an additional check will proceed, if there no link to the file – it will be
deleted. You can do it using relationships and
observers.
Let’s come back to our example with hero relation from “Manage file attachments”
// app/Post.php
use Orchid\Attachment\Models\Attachment;
public function hero()
{
return $this->hasOne(Attachment::class, 'id', 'hero')->withDefault();
}
If you call your relation like function $post->hero() it will return Illuminate\Database\Eloquent\Builder
class, that also has delete() function. But, it will execute sql query. If you call your relation like attribute
$post->hero it will return model class. Attachment model class.
$post->hero()->delete();
Note. You should build your relation using
withDefault()function to avoid the null pointer exception.
Let’s generate observer for our example model.
php artisan make:observer PostObserver
In PostObserver we create deleting function
public function deleting(Post $post)
{
$post->hero()->delete();
}
In case, when you have multiple attachments you should use attachment relation from the Attachable trait.
public function deleting(Post $post)
{
$post->attachment()->each->delete();
}
Subscribe example model to the observer in AppServiceProvider
public function boot()
{
...
Post::observe(PostObserver::class);
}
Subscribe to download
Different file processing options may require additional processing, such as video compression, This is possible thanks to an event that you can subscribe to using standard tools and perform a task in the background:
namespace App\Providers;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use App\Listener\UploadListener;
class EventServiceProvider extends ServiceProvider
{
/**
* The event handler mappings for the application.
*
* @var array
*/
protected $listen = [
UploadFileEvent::class => [
UploadListener::class,
],
];
/**
* Register any events for your application.
*/
public function boot()
{
parent::boot();
}
}
Each subscription will receive an object UploadFileEvent:
namespace App\Listeners;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Orchid\Platform\Events\UploadFileEvent;
class UploadListener extends ShouldQueue
{
use InteractsWithQueue;
/**
* Handle the event.
*
* @param UploadFileEvent $event
* @return void
*/
public function handle(UploadFileEvent $event)
{
//$event->attachment
//$event->time
}
}