Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

it-systemmanagement's avatar

Reuse form Elements in Create and Update Blade

Hello,

I would like to reuse my form fields in the create and update blades.

What i got so far is a:

create.blade.php edit.blade.php form.blade.php

edit.blade.php:

@extends('partner.form')

@section('name', $partner->name)
@section('phone', $partner->phone)
@section('partnerNumber', $partner->partner_number)
@section('street', $partner->street)
@section('zipcode', $partner->zipcode)
@section('city', $partner->city)
@section('country_id', $partner->country_id)
@section('country_region_id', $partner->country_region_id)
@section('insertTimestamp', $partner->insert_timestamp)
@section('insertUser' , $partner->insert_user)

form.blade.php:

for example:

<input type="text" name="street" placeholder="Adresse" value="@yield('street')">

like this i can populate my inputs if i render the edit.blade.php in my edit action, and leave them empty if i render the create.blade.php in the create action.

The problem i got is when i try to compare value, for example setting the value of a select/dropdown

as i cant compare the yielded values in IFs

@foreach($countries as $country)
<div class="item" data-value="{{$country->country_id}}"

@if ($country->country_id == @yield('country_id')) active @endif>
    <i class="{{ strtolower($country->alpha2) }} flag"></i>{{ $country->name }}</div>
@endforeach 

is there any other way to reuse forms and populate them?

0 likes
6 replies
tisuchi's avatar

Its because you use @ before yield.

Try-

@if ($country->country_id == "@yield('country_id')" ) active @endif>
    <i class="{{ strtolower($country->alpha2) }} flag"></i>
{{ $country->name }}</div>
@endforeach 
3 likes
it-systemmanagement's avatar

Thank you for you answer, but sadly it doesnt seem to work. I think laravel is comparing 172 with just the string -> "@yield('country_id')" and cant pull the content.

martinbean's avatar
Level 80

I still use the Laravel Collective forms package, primarily for it’s form–model binding so I don’t have to pollute my Blade views with things like multiple @​sections to populate form fields.

I’ll have a “form” template for whatever entity I’m editing, and then include that in both my create and edit views:

<!-- resources/views/admin/forms/product.blade.php -->
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
    {{ Form::label('name') }}
    <div class="form-controls">
        {{ Form::text('name', null, ['class' => 'form-control', 'required', 'autofocus']) }}
    </div>
</div>

<!-- And other fields -->
<!-- resources/views/admin/products/create.blade.php -->
@​extends('admin.layout')

@​section('content')
    <h1>Add Product</h1>
    {{ Form::open(['route' => 'admin.product.store']) }}
        @​include('admin.forms.product')
    {{ Form::close() }}
@​endsection
<!-- resources/views/admin/products/edit.blade.php -->
@​extends('admin.layout')

@​section('content')
    <h1>Edit Product</h1>
    {{ Form::model($product, ['route' => ['admin.product.update', $product], 'method' => 'PUT']) }}
        @​include('admin.forms.product')
    {{ Form::close() }}
@​endsection
it-systemmanagement's avatar

not sure how customizable Laravel Collective elements are. I am using semantic ui, and they got some special Dropdowns, with divs.

it-systemmanagement's avatar

@martinbean i think i am going to use your idea with the include('form'), but i will go without the Collective and do it manually.

<input type="text" class="form-control" name="my-input" value="{{ old('my-input',  isset($post->title) ? $post->title : null) }}" placeholder="My input" />

thx for your help guys.

martinbean's avatar

If you’re going down that route, then I’d maybe pass an “empty” model to your create view. Means you won’t have to do those ugly isset($post->title)-like checks in your views.

class PostController extends Controller
{
    public function create()
    {
        return view('post.create', [
            'post' => new Post,
        ]);
    }
}
1 like

Please or to participate in this conversation.