Last active
August 16, 2023 19:30
-
-
Save webdevmatics/11290b6e9ae75469f6f017741bfb679f to your computer and use it in GitHub Desktop.
Clone Edit Form #livewire
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div> | |
<div> | |
@if (session()->has('message')) | |
<div class="alert alert-success"> | |
{{ session('message') }} | |
</div> | |
@endif | |
</div> | |
<form wire:submit.prevent="store"> | |
@csrf | |
@for($i=0; $i< count($inputs) ; $i++) | |
<h3>Education {{$i+1}} </h3> | |
<div class="form-section"> | |
<input type="text" wire:model.lazy='inputs.{{$i}}.school_name' placeholder='school name'> | |
@error("inputs.{$i}.school_name") | |
<span class="error text-danger">Name required</span> | |
@endError | |
<br> | |
<input type="text" wire:model.lazy='inputs.{{$i}}.degree' placeholder='degree'> | |
<br> | |
<input type="text" wire:model.lazy='inputs.{{$i}}.field_of_study' placeholder='faculty'> | |
<br> | |
<input type="date" wire:model.lazy='inputs.{{$i}}.graduation_start_date'> | |
<br> | |
<input type="date" wire:model.lazy='inputs.{{$i}}.graduation_end_date'> | |
<br> | |
<button wire:click='remove({{$i}})' type="button" class=" btn btn-sm btn-danger m-2">-</button> | |
</div> | |
@endfor | |
<button wire:click="addNew" type="button" class=" btn btn-sm btn-primary mt-3">Add New</button> | |
<br> | |
<input type="submit" class="btn btn-success mt-4" value="Save Education"> | |
</form> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Livewire; | |
use App\Education; | |
use Livewire\Component; | |
class CloneForm extends Component | |
{ | |
public $inputs = []; | |
public function mount($educations = []) | |
{ | |
$this->addNew(); | |
if (!empty($educations)) { | |
$this->inputs = $educations; | |
} | |
} | |
public function render() | |
{ | |
return view('livewire.clone-form'); | |
} | |
public function addNew() | |
{ | |
$this->inputs[] = []; | |
} | |
public function remove($index) | |
{ | |
unset($this->inputs[$index]); | |
$this->inputs = array_values($this->inputs); | |
} | |
public function store() | |
{ | |
$this->validate([ | |
'inputs.*.school_name' => 'required', | |
'inputs.*.degree' => 'required', | |
'inputs.*.field_of_study' => 'required', | |
'inputs.*.graduation_start_date' => 'required', | |
'inputs.*.graduation_end_date' => 'required', | |
]); | |
foreach ($this->inputs as $input) { | |
if (!empty($input['id'])) { | |
Education::find($input['id'])->update($input); | |
} else { | |
auth()->user()->education()->create($input); | |
} | |
} | |
session()->flash('message', 'Saved successfully'); | |
return redirect()->route('education.index'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@extends('layouts.app') | |
@section('content') | |
<div class="container"> | |
<h2>Edit Education</h2> | |
@livewire('clone-form',['educations'=> auth()->user()->education->toarray()]) | |
</div> | |
@endsection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment