Laravel Validation Exclude_if Anotherfield,Value Example

Laravel 8

Nicesnippets

08-05-2021


Laravel Validation Exclude_if Anotherfield,Value Example

Hi guys, Today i will explained to the Laravel Validation Exclude_if Anotherfield,Value Example in your laravel project.Laravel Validation Exclude_if Anotherfield,Value Example is so easy to use.so you can just follow my step by step and learn Laravel Validation Exclude_if Anotherfield,Value Example.

So let's start to the example and follow to the my all step.

Solution


$data = $request->validate([

'has_doctor_appointment' => 'required|bool',

'pasant_name' => 'exclude_if:has_doctor_appointment,false|required|string',

'doctor_name' => 'exclude_if:has_doctor_appointment,false|required|string',

]);

Example :

Step 1: Create Route

Last step to create a route in web.php file and use this code.

Route :routes/web.php

<?php

use Illuminate\Support\Facades\Route;

use App\Http\Controllers\AppointmentController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

Route::get('appoiment/create', [AppointmentController::class, 'index'])->name('appoiment.create');

Route::post('appoiment/store', [AppointmentController::class, 'store'])->name('appoiment.store');

Step 2: Create a AppointmentController Controller

Next you can require to the User Controller so create a User Controller in just following command through.

Controller : app/Http/Controllers/AppointmentControllerController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Appointment;

class AppointmentController extends Controller

{

/**

* The attributes that are mass assignable.

*

* @var array

*/

public function index()

{

return view('appointment');

}

public function store(Request $request)

{

$data = $request->validate([

'has_doctor_appointment' => 'required|bool',

'pasant_name' => 'exclude_if:has_doctor_appointment,false|required|string',

'doctor_name' => 'exclude_if:has_doctor_appointment,false|required|string',

]);

dd('done');

}

}

Step 4: Create a Appointment Blade File

Next you can require to the appointment.blade.php so create a appointment.blade.php in just following command through.

View :resources/views/appointment.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel Validation Exclude_if Anotherfield,Value Example</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

</head>

<body>

<div class="container">

<h1>Laravel Validation Exclude_if Anotherfield,Value Example</h1>

<div class="card-body">

@if (count($errors) > 0)

<div class="row">

<div class="col-md-12">

<div class="alert alert-danger alert-dismissible">

<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>

@foreach($errors->all() as $error)

{{ $error }} <br>

@endforeach

</div>

</div>

</div>

@endif

{!! Form::open(array('route' => 'appoiment.store','method'=>'POST')) !!}

<div class="row">

<div class="col-md-12">

<div class="form-group">

<label>Pasant Name</label>

<input type="text" class="form-control" placeholder="Pasant Name" name="pasant_name">

</div>

</div>

</div>

<div class="row">

<div class="col-md-12">

<div class="form-group">

<label>Doctor Name</label>

<input type="text" class="form-control" placeholder="Doctor Name" name="doctor_name">

</div>

</div>

</div>

<div class="custom-control custom-radio">

<input type="radio" value="1" class="custom-control-input" id="defaultUnchecked" name="has_doctor_appointment">

<label class="custom-control-label" for="defaultUnchecked">Yes</label>

</div>

<div class="custom-control custom-radio">

<input type="radio" value="0" class="custom-control-input" id="defaultChecked" name="has_doctor_appointment" checked>

<label class="custom-control-label" for="defaultChecked">No</label>

</div>

<div class="row">

<div class="col-md-12 text-center mt-2 mb-3">

<button type="submit" class="btn btn-success">Submit</button>

</div>

</div>

{!! Form::close() !!}

</div>

</div>

</body>

</html>

So, finally we are done with our code we can get below output.

php artisan serve

Browser url run : http://localhost:8000/appoiment/create