Route:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\FlightController;
Route::get('/', function () {
return view('welcome');
});
Route::get('/flight-create',[FlightController::class,'create'])->name('flight.create');
Route::post('/flight-store',[FlightController::class,'store'])->name('flight.store');
HTML Form:
<div class="container">
<div class="row">
<div class="col-md-4">
@if ($errors->any())
@foreach ($errors->all() as $element)
<p>{{ $element }}</p>
@endforeach
@endif
<form action="{{ route('flight.store') }}" method="post">
@csrf
<div class="mb-3">
<label for="firstName" class="form-label">First Name</label>
<input type="text" class="form-control form-control-sm" id="firstName" name="first_name">
@if($errors->has('first_name'))
<div class="help-text">{{ $errors->first('first_name') }}</div>
@endif
</div>
<div class="mb-3">
<label for="lastName" class="form-label">Last Name</label>
<input type="text" class="form-control form-control-sm" name="last_name" id="lastName">
</div>
<div class="mb-3">
<label for="phone1" class="form-label">Phone1</label>
<input type="text" class="form-control form-control-sm" name="phone1" id="phone1">
</div>
<div class="mb-3">
<label for="phone2" class="form-label">Phone2</label>
<input type="text" class="form-control form-control-sm" name="phone2" id="phone2">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
Form Request:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class StoreFlightRequest extends FormRequest {
public function authorize() {
return true;
}
public function rules() {
return [
'first_name' =>'required',
'last_name' =>'required',
'phone_number' =>'nullable|string',
'phone1' => 'required|exclude',
'phone2' => 'required|exclude',
];
}
protected function prepareForValidation() {
$this->merge([
'phone_number' => $this->phone1 . $this->phone2,
]);
}
}
Flight Controller:
<?php
namespace App\Http\Controllers;
use App\Models\Flight;
use App\Http\Requests\StoreFlightRequest;
use App\Http\Requests\UpdateFlightRequest;
class FlightController extends Controller {
public function index() {
}
public function create() {
return view('flight.create');
}
public function store(StoreFlightRequest $request) {
$postData = $request->validated();
dd($postData);
}
public function show(Flight $flight) {
}
public function edit(Flight $flight) {
}
public function update(UpdateFlightRequest $request, Flight $flight) {
}
public function destroy(Flight $flight){
}
}
OUTPUT:
array:3 [▼
"first_name" => "Chandra"
"last_name" => "Prakash"
"phone_number" => "9812098485636252255"
]
0 comments:
Post a Comment