Request Data Retrirval

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

जो सफर की शुरुआत करते हैं,

वो मंज़िल को पार करते हैं,


एकबार चलने का होंसला तो रखो,
मुसाफफरों का तो रस्ते भी इंत़िार करते
हैं।
How was your MidTerm
Examination?
Request Data Retrieval
Akash Pundir
Create a Controller:
• Generate a new controller to handle the request and display the data.
Run the following command:

php artisan make:controller RequestDemoController


Create Routes:
• Define two routes in the routes/web.php file, one to display the form
and another to handle the form submission:

• Route::get('/form', [RequestDemoController::class,
'showForm']);
• Route::post('/process',
[RequestDemoController::class, 'processForm']);
• Create a Blade view file named form.blade.php in the resources/views directory to display the form:
• <!DOCTYPE html>
• <html>
• <head>
• <title>Request Data Retrieval Demo</title>
• </head>
• <body>
• <h2>User Information Form</h2>
• <form method="post" action="/process">


@csrf
<label for="name">Name:</label>
Create the Form View:
• <input type="text" name="name" id="name"><br><br>

• <label for="email">Email:</label>
• <input type="email" name="email" id="email"><br><br>

• <input type="submit" value="Submit">
• </form>
• </body>
• </html>
Create the Controller Methods:
public function showForm()
{
return view('form');
}

public function processForm(Request $request)


{
// Retrieve input data
$name = $request->input('name');
$email = $request->input('email’);
$allData = $request->all();

// Display the data on the confirmation page


return view('confirmation', compact('name', 'email’,’allData’));
}
Create the Confirmation View:
Create a Blade view file named confirmation.blade.php in the resources/views directory to display the submitted data:

<!DOCTYPE html>
<html>
<head>
<title>Request Data Retrieval Demo - Confirmation</title>
</head>
<body>
<h2>Confirmation</h2>
<p>Name: {{ $name }}</p>
<p>Email: {{ $email }}</p>
<hr>
<h3>All Input Data:</h3>
<pre>{{ print_r($allData, true) }}</pre>

</body>
</html>
Old Input
Old input refers to the ability to retrieve and
display previously submitted form data, often
used in forms that need to be redisplayed with
user-submitted data after a validation error
occurs.
Laravel provides a convenient way to access old
input data using the old() function.
Let’s make a new controller for this.

php artisan make:controller


RegistrationController
Now let’s make a registration form view.
<!DOCTYPE html>
<html>
<head>
<title>User Registration</title>
</head>
<body>
<h2>User Registration</h2>
@if ($errors->any())
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
@endif
Almost completed….
<form method="post" action="/register">
@csrf
<label for="name">Name:</label>
<input type="text" name="name" id="name" value="{{ old('name') }}"><br><br>

<label for="email">Email:</label>
<input type="email" name="email" id="email" value="{{ old('email') }}"><br><br>

<label for="password">Password:</label>
<input type="password" name="password" id="password"><br><br>

<label for="password_confirmation">Confirm Password:</label>


<input type="password" name="password_confirmation" id="password_confirmation"><br><br>

<input type="submit" value="Register">


</form>
</body>
</html>
Now, at backend we need to process the data, in
RegistrationController make new functions
public function showRegistrationForm()
{
return view('registration');
}

public function processRegistration(Request $request)


{
// Define validation rules
$rules = [
'name' => 'required|string|max:255',
'email' => 'required|email|max:255',
'password' => 'required|string|min:6|confirmed',
];

// Validate the request data


$validator = Validator::make($request->all(), $rules);
It’s not completed keep typing…
if ($validator->fails()) {
// If validation fails, redirect back to the registration form with old input
return redirect('/register')
->withErrors($validator)
->withInput();
}

// Registration logic (not saving to the database in this example) would go here
// ...

// If registration is successful, redirect to a success page


return view('confirmation', [
'name' => $request->input('name'),
'email' => $request->input('email'),
'allData' => $request->all()
]);
}
Are you forgetting something?

use Illuminate\Support\Facades\Validator;
Now, let’s define routes to access our pages in
web.php

Route::get('/register', [RegistrationController::class,
'showRegistrationForm’]);

Route::post('/register', [RegistrationController::class,
'processRegistration']);
Always remember to include the controller
before using it.

use App\Http\Controllers\RegistrationController;

You might also like