0% found this document useful (0 votes)
24 views19 pages

Request Data Retrirval

Here are the steps to implement old input in Laravel: 1. Create a RegistrationController 2. Define routes to show the registration form and process submission 3. Create a registration form view 4. In the view, use {{ old('field') }} to repopulate fields on validation error 5. In the controller, validate the request data 6. If validation fails, redirect back to form with errors and old input 7. Otherwise, process successful registration The key points are: - Use {{ old('field') }} in views to repopulate fields - On validation error, redirect back to form with $errors and $request->old() - This allows fields to be repopulated

Uploaded by

Ujjwal Shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views19 pages

Request Data Retrirval

Here are the steps to implement old input in Laravel: 1. Create a RegistrationController 2. Define routes to show the registration form and process submission 3. Create a registration form view 4. In the view, use {{ old('field') }} to repopulate fields on validation error 5. In the controller, validate the request data 6. If validation fails, redirect back to form with errors and old input 7. Otherwise, process successful registration The key points are: - Use {{ old('field') }} in views to repopulate fields - On validation error, redirect back to form with $errors and $request->old() - This allows fields to be repopulated

Uploaded by

Ujjwal Shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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