I can't see how it would be bad practice. You do you :)
That being said, I would, personally, not do it as I really like to make my markup myself. Plus that would make implementing a frontend Framework more difficult.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
One thing that bugged me a little with Laravel is how we specify form input fields directly in the view.
I come from a CodeIgniter background where generally you define your form input fields from the controller.
What I have done in Laravel is written a little helper function called formInput() which takes an array, defined in the controller, to generate form input fields in the view.
Here's an example:
class CustomerController extends Controller
{
// Show the create a new customer form
public function create()
{
// Define the fields for our view
$fields = [
'company' => [
'type' => 'text',
'name' => 'company',
'placeholder' => 'Enter company name',
'required' => 'required',
'maxlength' => 35,
'value' => old('company'),
'class' => 'form-input',
],
];
// Return the view
return view('customer/edit', [
'fields' => $fields,
]);
}
}
And then in my view I can do this:
<!-- Display the company form input -->
{!! formInput($fields['company']) !!}
I have omited my formInput() helper function for brevity, but essentially this looks at all the attributes passed to it and generates an input tag with those attributes.
Is there any issue with this? A good reason not to do it? It just feels like defining the form input, along with its max length, whether it's required or not, is really a job for the controller and not for the view.
What do you think?
Here's the solution I've come up with:
Controller
use App\Http\Requests\CustomerUpdateRequest;
use App\Http\Fields\CustomerEditFields;
class CustomerController extends Controller
{
// Type hint the fields we want
public function edit(Customer $customer, CustomerEditFields $fields)
{
return view('customer/edit', [
'customer' => $customer,
'fields' => $fields->fields(),
]);
}
// Type hint form validation request
public function update(Customer $customer, CustomerUpdateRequest $request)
{
// Update the customer
$customer->update($request->validated());
// Redirect back
return redirect()->back()->with([
'success' => 'Customer saved',
]);
}
}
I then create a FormRequest class under \App\Http\Requests which is pretty standard.
However I've created my own CustomerEditFields class under \App\Http\Fields which defines and returns the fields that I want to the edit controller, which in turn passes them to the view.
My CustomerEditFields class looks like this:
namespace App\Http\Fields;
class CustomerEditFields
{
public function fields() {
return [
'company' => [
'type' => 'text',
'name' => 'company',
'placeholder' => 'Enter company name',
'required' => 'required',
'maxlength' => 35,
'value' => old('company'),
'class' => 'form-input' ],
];
}
}
Any thoughts on whether this is an acceptable, or terrible, solution?
Please or to participate in this conversation.