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

scala's avatar
Level 1

form post action in laravel

I am from ASP.NET background. There we used to create form in master page then in every child page we have to create only required fields of the particular page. We do not have to mention action tag in the form for this. The form is submitted by calling the controller class from the code behind page of the child aspx page for the asp button control. So we don't have to create form tag in every page.

Here in laravel, can I do the same thing? I searched in internet but can not found whether it can be done or not? If it can be done in Laravel too, how can I do it? Thank You!!!

0 likes
4 replies
lostdreamer_nl's avatar

If I understand correctly, then yes, this is the same in PHP.

Simply create a form.blade.php with the open / closing form tags and submit / reset buttons.

<form method="post">

@yield('fields')

<br />

<input type="submit" value="Save">
</form>

Then create a forms/my-form.blade.php

@extends('form')

@section('fields')

    <input type="text" name="username">

@endsection

Now, when loading view('forms/my-form'); you will see your complete form.

scala's avatar
Level 1

@lostdreamer_nl thank you for the response. I've created the view page keeping form tag in master page. But I want to know how can I post the data to the database using this concept. Since in master page we can not keep action tag. Thank You!!!

lostdreamer_nl's avatar

without an action="url-here" the form will always post back to it's own URL. So the only thing to do is make sure that the GET on that URL will show the form, and the POST will handle it.

When you need to update, you can use an extra formfield:

<input name="_method" type="hidden" value="PUT">

this way you can use the same route, but with put / delete / patch.

newbie360's avatar

use blade component

@extends('master')

@section('form')
    @component('cp.form')
        @slot('form_action', route('route_name'))
        @slot('method', 'PATCH')
        @slot('input_name', 'user_name')

        if you want use $slot, put the code here
    @endcomponent
@endsection

Component

// views/cp/form.blade.php
<form method="POST" action="{{ $form_action }}">
    @isset($method)
        @method( {{$method}} )
    @endisset
    
    @csrf
    <input type="text" id="{{ $input_name }}" name="{{ $input_name }}" value="">
    <button type="submit">Save</button>
</form>

{{ $slot }}

Please or to participate in this conversation.