PHP code example of dongttfd / laravel-upload-model
1. Go to this page and download the library: Download dongttfd/laravel-upload-model 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/ */
dongttfd / laravel-upload-model example snippets
namespace App\Models;
use DongttFd\LaravelUploadModel\Contracts\UploadOnEloquentModel;
use DongttFd\LaravelUploadModel\Eloquent\UploadFileEloquent;
class User extends Model implements UploadOnEloquentModel
{
use UploadFileEloquent;
...
}
...
// $file must is instance of Illuminate\Http\UploadedFile
$file = $request->file('avatar');
User::create(['avatar' => $file]);
...
// $file must is instance of Illuminate\Http\UploadedFile
$file = $request->file('avatar');
$user->update(['avatar' => $file]);
...
// $file must is instance of Illuminate\Http\UploadedFile
$file = $request->file('avatar');
$user->update(['avatar' => $file]);
// path of file
$user->avatar;
// url of file
$user->avatar_url;
namespace App\Models;
use DongttFd\LaravelUploadModel\Contracts\UploadOnEloquentModel;
use DongttFd\LaravelUploadModel\Eloquent\UploadFileEloquent;
use Illuminate\Database\Eloquent\Model;
class BaseFileModel extends Model implements UploadOnEloquentModel
{
use UploadFileEloquent;
}
namespace App\Models;
use DongttFd\LaravelUploadModel\Eloquent\FileModel;
class User extends FileModel
{
}
/**
* Default save on disk (from keys of app/config/filesystem.php > disks)
*
* @var string
*/
protected $saveOnDisk = null;
/**
* Save file to avatar columns
*
* @var array
*/
protected $fileFields = ['avatar'];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'card' => 'array',
];
/**
* Save files to photos columns
*
* @var array
*/
protected $fileFields = [
'card.front',
'card.back'
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'photos' => 'array',
];
/**
* Save files to photos columns
*
* @var array
*/
protected $fileFields = ['photos.*'];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'variant' => 'array',
];
/**
* Save files to photos columns
*
* @var array
*/
protected $fileFields = ['variant.photos.*'];