PHP code example of yassinedabbous / laravel-file-cast
1. Go to this page and download the library: Download yassinedabbous/laravel-file-cast 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/ */
yassinedabbous / laravel-file-cast example snippets
use YassineDabbous\FileCast\FileCast;
class User extends Model
{
# Laravel<11.x
protected $casts = [
'avatar' => FileCast::class, // use default disk
'avatar' => FileCast::class.':s3,photo.png', // use "S3" disk and "photo.png" as default value
];
# OR
# Laravel >=11.x
public function casts(): array
{
return [
'avatar' => FileCast::class, // use default disk
'avatar' => FileCast::using(disk: 's3', default: 'photo.png'), // use S3 disk and "photo.png" as default value
'avatar' => FileCast::using(disk: $this->disk_column), // use column value as a disk name
];
}
}
class Post extends Model
{
...
/** Set a disk for each column */
public function disks(): array {
return [
'photo' => $this->disk_column, # use column value as a disk
'video' => $migrated ? 's3' : 'public', # conditional disk
];
}
...
}
use YassineDabbous\FileCast\HasFileCast;
class User extends Model
{
use HasFileCast;
...
}
$model->avatar = NULL; # Delete the file without updating table.
$model->avatar->delete(); # Delete the file without updating table.
$model->avatar->delete(persist: TRUE); # Delete the file & save changes to DB.
$model->avatar;
# 'folder/old_file.png';
$model->avatar = '@new_folder/new_file.png'; # Move the file without updating table. (path should start with "@")
$model->avatar->move(to: 'new_folder/new_file.png'); # Move the file without updating table.
$model->avatar->move(to: 'new_folder/new_file.png', persist: TRUE); # Move the file & save changes to DB.
return [
/** Default value when no file uploaded. */
'default' => env('FILE_CAST_DEFAULT'),
/** Default storage disk */
'disk' => env('FILE_CAST_DISK'),
/** Default storage folder. If NULL, the Model's table name will be used. */
'folder' => env('FILE_CAST_FOLDER'),
/** Automatically clean files on column value updated. */
'auto_delete' => env('FILE_CAST_AUTO_DELETE', true),
/** Serialize attribute to full URL. */
'serialize_to_full_url' => env('FILE_CAST_SERIALIZE_TO_FULL_URL', true),
];
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.