forked from eatvio/eatvio-infra
68 lines
1.8 KiB
PHP
68 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Diary;
|
|
|
|
use App\Models\Meal;
|
|
use App\Models\UserAttachment;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Livewire\Component;
|
|
use Livewire\WithFileUploads;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class MealPhotos extends Component
|
|
{
|
|
use WithFileUploads;
|
|
|
|
public Meal $meal;
|
|
public $tmpMealPhotos = [];
|
|
|
|
public function render()
|
|
{
|
|
return view(
|
|
'livewire.diary.meal-photos',
|
|
[
|
|
'curMealPhotos' => $this->meal->photos
|
|
],
|
|
);
|
|
}
|
|
public function updatedTmpMealPhotos($values)
|
|
{
|
|
/* dd($values); */
|
|
$newPhotos = collect($values)->take(3);
|
|
$attachmentGroup = "meal_photos";
|
|
$userId = Auth::user()->id;
|
|
|
|
$filePath = $attachmentGroup . "/" . $userId . "/" . $this->meal->id;
|
|
$publicFilePath = "public/" . $filePath;
|
|
|
|
foreach($newPhotos as $photo) {
|
|
|
|
$curPhoto = $photo
|
|
->storePublicly($publicFilePath);
|
|
|
|
$fileData = [
|
|
'user_id' => $userId,
|
|
'group' => $attachmentGroup,
|
|
'original_name' => $photo->getClientOriginalName(),
|
|
'size' => $photo->getSize(),
|
|
];
|
|
|
|
$newFile = UserAttachment::updateOrCreate(
|
|
$fileData,
|
|
[
|
|
'path' => preg_replace("/public\//xi", "/storage/", $curPhoto),
|
|
'specs' => json_encode(
|
|
[
|
|
'ext' => $photo->extension(),
|
|
'original_path' => $curPhoto,
|
|
]
|
|
)
|
|
]
|
|
);
|
|
|
|
$newFile->save();
|
|
$this->meal->photos()->attach($newFile->id);
|
|
}
|
|
}
|
|
}
|