PHP code example of phoenixfire296 / laravel-attachments
1. Go to this page and download the library: Download phoenixfire296/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/ */
phoenixfire296 / laravel-attachments example snippets
namespace App;
use Bnb\Laravel\Attachments\HasAttachment;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable, HasAttachment;
// ...
}
$user = App\User::first();
$attachmentByKey = $user->attachment('myKey');
$attachmentByKey->delete(); // Will also delete the file on the storage by default
use Bnb\Laravel\Attachments\Attachment;
class AppServiceProvider extends ServiceProvider
{
// ...
public function boot() {
// ...
Attachment::outputting(function ($attachment) {
/** @var Attachment $attachment */
// Get the related model
$model = $attachment->model;
if (empty($model)) {
// Deny output for attachments not linked to a model
return false;
}
if ($model instanceof \App\User) {
// Check if current user is granted and owner
$user = \Auth::user();
return $user && $user->can('download-file') && $user->id == $model->id;
}
});
// ...
}
// ...
}