Introduction
Form inputs let users send information to your website. Laravel helps you create and handle these inputs easily.
Form inputs let users send information to your website. Laravel helps you create and handle these inputs easily.
<?php // In Blade template <form method="POST" action="/submit"> @csrf <input type="text" name="username" /> <input type="submit" value="Send" /> </form>
@csrf to add a security token automatically.name attribute is important to identify the input data.<input type="text" name="email" placeholder="Enter your email" />
<input type="password" name="password" />
<textarea name="message" rows="4" cols="50"></textarea>
<select name="country"> <option value="us">USA</option> <option value="ca">Canada</option> </select>
This example shows a simple contact form. When the user submits their name and message, Laravel receives it and replies with a greeting message.
<?php // routes/web.php use Illuminate\Http\Request; Route::get('/contact', function () { return view('contact'); }); Route::post('/contact', function (Request $request) { $name = $request->input('name'); return "Hello, $name! Your message was received."; }); // resources/views/contact.blade.php ?><form method="POST" action="/contact"> @csrf <label for="name">Name:</label> <input type="text" id="name" name="name" required /> <br /> <label for="message">Message:</label> <textarea id="message" name="message" required></textarea> <br /> <input type="submit" value="Send" /> </form>
Always include @csrf in your forms to protect against attacks.
Use the name attribute to access input values in your controller or route.
Validate inputs on the server side to keep data safe and clean.
Form inputs let users send data to your Laravel app.
Use Blade templates with @csrf and name attributes.
Handle input data in routes or controllers using the Request object.
name attribute to an input field in a Laravel form?name attributename attribute is used to label the input data so the server can identify it when the form is submitted.name = data key [OK]name with styling or placeholdername disables inputname causes missing data on submit@csrf inside forms to add a hidden CSRF token input automatically.@csrf is valid; other tags like <csrf> or <token> are invalid HTML or Blade syntax.@csrf in Blade forms [OK]<form method="POST" action="/submit"> @csrf <input type="text" name="username" value="John"> <button type="submit">Send</button> </form>
request('username') return in the controller after submission?name="username" and value="John", so the form sends "John" as the username.request() worksrequest('username') retrieves the submitted value for the input named "username".<form method="POST" action="/save"> <input type="text" name="email"> </form>
@csrf.type="number" with min="18" and max="99" restricts input to numbers in that range.required to ensure user cannot submit emptyPass the test and this pattern moves unknown