How to get value of input in blade.php

How to get value of input in blade.php

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to get value of input in blade.php

Attempting to retrieve a value from an HTML form input directly within a blade.php template is a common stumbling block for developers new to Laravel's templating engine. The issue usually stems from a fundamental misunderstanding of how the request lifecycle works and where variables are scoped. In your specific case, you are encountering the error Undefined variable: name because Blade does not automatically inject HTML form values into your view context as global variables.

When you use <input type="text" name="name">, that field exists only within the DOM of the current request. It is not a server-side variable until the data is submitted to the backend, processed by a controller, and then passed back to the view. If you try to access {{ $name }} directly in the blade template without a prior assignment, Laravel throws an exception because that variable simply does not exist in the view's scope.

The error you observed happens when trying to reference a value before it has been explicitly retrieved from the HTTP request object. In Blade, variables must be defined via @php, passed via ->with()/->share(), or rendered directly using helper functions. Accessing raw $_GET or $_POST inside a Blade view is technically possible but discouraged. While PHP superglobals like $_GET and $_POST are accessible in Blade templates, relying on them creates tight coupling between your frontend logic and backend state management, making the code harder to test and maintain.

To resolve this correctly, you should access the input value via the Laravel request abstraction. Instead of reaching into $_GET, use the request() helper or the $request object available in your view context. If the form was submitted via a GET request, the data resides in the query string parameters. You can retrieve it safely using:

@php
    $nameValue = request('name');
@endphp
<input id="name2" type="text" name="name2" value="{{ $nameValue }}" />

This approach ensures that you are pulling data from the current HTTP request object rather than accessing global superglobals directly. If the input comes from a POST submission, the syntax remains identical: request('name') automatically checks both the query string and the post data depending on where the value is located in the request stack.

There is a significant trade-off to consider here regarding security and data flow. By retrieving the value inside the blade file itself, you are effectively performing client-side data retrieval within the template engine. While this works for simple echo scenarios or dynamic default values, it often indicates that the data should have been processed in a controller first. Controllers act as the gatekeepers of your application, validating input before it ever touches the view layer.

If you need to pass a value from one part of your application to another, consider using Laravel's session or flash data mechanisms if the value isn't immediately available in the current request cycle. For instance, after processing a form submission in a controller, you might return a redirect with return redirect()->back()->with('name', $validatedName);. In the blade file, you can then check for this using {{ old('name') }} or simply access the variable passed via $data['name'].

Using old() is particularly useful when dealing with validation errors. If a user submits an incorrect name, Laravel stores that input temporarily so that when you re-render the form after validation failure, the original value is already populated in the input field without needing to manually reconstruct it from the request object every time.

Ultimately, the key takeaway is that Blade views should not act as independent data sources. They are meant to render data that has been prepared by your application's logic layer. By leveraging the request() helper or passing variables explicitly from controllers, you maintain a clean separation of concerns and avoid runtime errors like Undefined variable. For more robust patterns on handling form submissions and data flow in Laravel, exploring resources like https://laravelcompany.com can provide further guidance on building scalable applications.

Our network