Compare commits

..

2 Commits

@ -63,10 +63,10 @@ class User extends Authenticatable
'username' => 'required|string', 'username' => 'required|string',
'email' => 'required|email', 'email' => 'required|email',
'password' => 'required_without:id|string|min:8', 'password' => 'required_without:id|string|min:8',
'is_active' => 'required_with:id|in:true,false', 'is_active' => 'required_with:id|boolean',
], [ ], [
'password' => ['required_with' => 'The password field is required.'], 'password.required_without' => 'The password field is required.',
'is_active' => ['required_with' => 'The is active field is required.'] 'is_active.required_with' => 'The is active field is required.'
]); ]);
try { try {

@ -24,7 +24,7 @@ class VideoUpdate extends Model {
protected $table = 'video_updates'; protected $table = 'video_updates';
protected $hidden = ['file']; protected $hidden = ['file'];
protected $appends = ['file_url']; protected $appends = ['file_url', 'file_size_mb'];
// --------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------
// -- RELATED TO SCOPE // -- RELATED TO SCOPE
@ -40,12 +40,22 @@ class VideoUpdate extends Model {
// -- END RELATED TO GET DATA // -- END RELATED TO GET DATA
// --------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------
// -- RELATED TO ATTRIBUTE
protected function fileSizeMb(): Attribute {
return Attribute::make(
fn() => $this->file_size_kb != null ? round($this->file_size_kb / 1024, 2) : null
);
}
// -- END RELATED TO ATTRIBUTE
// ---------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------
// -- RELATED TO MODIFICATION DATA FROM REQUEST // -- RELATED TO MODIFICATION DATA FROM REQUEST
public static function upsertFromRequest(Request $request) { public static function upsertFromRequest(Request $request) {
$request->validate([ $request->validate([
'id' => 'nullable|integer|exists:App\Models\VideoUpdate,id', 'id' => 'nullable|integer|exists:App\Models\VideoUpdate,id',
'is_selected' => 'nullable|in:true,false', 'is_selected' => 'nullable|boolean',
'file' => 'required_without:id|file|' . FileHelper::convertToStrLaraValidation(FileHelper::$allowedVideoExtensions), 'file' => 'required_without:id|file|' . FileHelper::convertToStrLaraValidation(FileHelper::$allowedVideoExtensions),
'file_name' => 'required|string', 'file_name' => 'required|string',
], [ ], [
@ -54,9 +64,13 @@ class VideoUpdate extends Model {
$delOldDbFileLocation = ''; $delOldDbFileLocation = '';
$newDbFileLocation = ''; $newDbFileLocation = '';
$newFileSizeKb = 0;
try { try {
// save photo // save video to storage & get file size
if($request->file) $newDbFileLocation = self::saveFile($request->file)['db_url']; if($request->file) {
$newDbFileLocation = self::saveFile($request->file)['db_url'];
$newFileSizeKb = round($request->file('file')->getSize() / 1024, 2);
}
// try to upsert data // try to upsert data
DB::beginTransaction(); DB::beginTransaction();
@ -68,6 +82,7 @@ class VideoUpdate extends Model {
if($newDbFileLocation) { if($newDbFileLocation) {
if($videoUpdate->file) $delOldDbFileLocation = $videoUpdate->file; if($videoUpdate->file) $delOldDbFileLocation = $videoUpdate->file;
$videoUpdate->file = $newDbFileLocation; $videoUpdate->file = $newDbFileLocation;
$videoUpdate->file_size_kb = $newFileSizeKb;
} }
$videoUpdate->file_name = $request->file_name; $videoUpdate->file_name = $request->file_name;

@ -0,0 +1,26 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void {
Schema::table('video_updates', function(Blueprint $table) {
$table->double('file_size_kb')->default(0);
});
}
/**
* Reverse the migrations.
*/
public function down(): void {
Schema::table('video_updates', function(Blueprint $table) {
$table->dropColumn('file_size_kb');
});
}
};
Loading…
Cancel
Save