mirror of
https://github.com/Einundzwanzig-Podcast/einundzwanzig-portal.git
synced 2025-12-11 06:46:47 +00:00
Shift automatically applies the Laravel coding style - which uses the PSR-12 coding style as a base with some minor additions. You may customize the code style applied by configuring [Pint](https://laravel.com/docs/pint), [PHP CS Fixer](https://github.com/FriendsOfPHP/PHP-CS-Fixer), or [PHP CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) for your project root. For more information on customizing the code style applied by Shift, [watch this short video](https://laravelshift.com/videos/shift-code-style).
77 lines
1.8 KiB
PHP
77 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Livewire\BookCase\Form;
|
|
|
|
use App\Models\BookCase;
|
|
use App\Models\Country;
|
|
use App\Models\OrangePill;
|
|
use Livewire\Component;
|
|
use Livewire\WithFileUploads;
|
|
|
|
class OrangePillForm extends Component
|
|
{
|
|
use WithFileUploads;
|
|
|
|
public Country $country;
|
|
|
|
public BookCase $bookCase;
|
|
|
|
public ?OrangePill $orangePill = null;
|
|
|
|
public $image;
|
|
|
|
public string $fromUrl = '';
|
|
|
|
protected $queryString = ['fromUrl' => ['except' => '']];
|
|
|
|
public function rules()
|
|
{
|
|
return [
|
|
'orangePill.book_case_id' => 'required',
|
|
'orangePill.user_id' => 'required',
|
|
'orangePill.amount' => 'required|numeric',
|
|
'orangePill.date' => 'required|date',
|
|
'orangePill.comment' => 'required|string',
|
|
'image' => 'image|max:8192', // 8MB Max
|
|
];
|
|
}
|
|
|
|
public function mount()
|
|
{
|
|
if (! $this->orangePill) {
|
|
$this->orangePill = new OrangePill([
|
|
'user_id' => auth()->id(),
|
|
'book_case_id' => $this->bookCase->id,
|
|
'date' => now(),
|
|
'amount' => 1,
|
|
]);
|
|
} elseif ($this->orangePill->user_id !== auth()->id()) {
|
|
abort(403);
|
|
}
|
|
}
|
|
|
|
public function save()
|
|
{
|
|
$this->validate();
|
|
$this->orangePill->save();
|
|
$this->orangePill
|
|
->addMedia($this->image)
|
|
->usingFileName(md5($this->image->getClientOriginalName()).'.'.$this->image->getClientOriginalExtension())
|
|
->toMediaCollection('images');
|
|
|
|
return redirect($this->fromUrl);
|
|
}
|
|
|
|
public function deleteMe()
|
|
{
|
|
$this->orangePill->delete();
|
|
|
|
return redirect($this->fromUrl);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.book-case.form.orange-pill-form');
|
|
}
|
|
}
|