| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- <?php
- namespace App\Http\Livewire;
- use Livewire\Component;
- class Card extends Component
- {
- public $cards, $name, $enabled, $next_day_expire, $next_month_expire, $cardId, $updateCard = false, $addCard = false;
- protected $listeners = [
- 'deleteCardListner'=>'deleteCard'
- ];
- protected $rules = [
- 'name' => 'required'
- ];
- public function resetFields(){
- $this->name = '';
- $this->next_day_expire = '';
- $this->next_month_expire = '';
- $this->enabled = true;
- }
- public function render()
- {
- $this->cards = Card::select('id', 'name', 'enabled')->get();
- return view('livewire.card');
- }
- public function addCard()
- {
- $this->resetFields();
- $this->addCard = true;
- $this->updateCard = false;
- }
- public function storeCard()
- {
- $this->validate();
- try {
- Card::create([
- 'name' => $this->name,
- 'next_day_expire' => $this->$next_day_expire,
- 'next_month_expire' => $this->$next_month_expire,
- 'enabled' => $this->enabled
- ]);
- session()->flash('success','Tessera creata');
- $this->resetFields();
- $this->addCard = false;
- } catch (\Exception $ex) {
- session()->flash('error','Errore');
- }
- }
- public function editCard($id){
- try {
- $card = Card::findOrFail($id);
- if( !$card) {
- session()->flash('error','Tessera non trovata');
- } else {
- $this->name = $card->name;
- $this->enabled = $card->enabled;
- $this->next_day_expire = $card->next_day_expire;
- $this->next_month_expire = $card->next_month_expire;
- $this->cardId = $card->id;
- $this->updateCard = true;
- $this->addCard = false;
- }
- } catch (\Exception $ex) {
- session()->flash('error','Errore');
- }
- }
- public function updateCard()
- {
- $this->validate();
- try {
- Card::whereId($this->cardId)->update([
- 'name' => $this->name,
- 'next_day_expire' => $this->$next_day_expire,
- 'next_month_expire' => $this->$next_month_expire,
- 'enabled' => $this->enabled
- ]);
- session()->flash('success','Tessera aggiornata');
- $this->resetFields();
- $this->updateCard = false;
- } catch (\Exception $ex) {
- session()->flash('success','Errore');
- }
- }
- public function cancelCard()
- {
- $this->addCard = false;
- $this->updateCard = false;
- $this->resetFields();
- }
- public function deleteCard($id)
- {
- try{
- Card::find($id)->delete();
- session()->flash('success',"Tessera eliminata");
- }catch(\Exception $e){
- session()->flash('error',"Errore");
- }
- }
- }
|