i am biginner , in body of html i have a input that filled by table row clicking:
<input id="office_id" disabled>
i want to place diffrent tag in body by value of office_id,if has value ,call PUT method and if is null value in office_id call POST method:
@if (?????)
<form method="POST" action="{{ route('office.store') }}">
<button type="submit" id="saveBtn" class="btn btn-warning" style="width: 95px;"> save</button>
</form>
@else
<form method="PUT" action="{{ route('office.update',$office->office_id) }}">
<button type="submit" id="saveBtn" class="btn btn-warning" style="width: 95px;"> update</button>
</form>
@endif
What code should I write inside the if condition?
2
Answers
In this code the empty() function is used to check if the office_id is empty or not. If it’s empty, it means you’re creating a new office, so the form uses the POST method and directs to the office.store route. If it’s not empty, it means you’re updating an existing office, so the form uses the POST method with the @method(‘PUT’) directive to override the method and directs to the office.update route with the specific office_id.
Your code has a few problems besides your question: I will try addressing both in my answer:
Security
Whenever working with forms, always make sure
@csrf
is present in your form to prevent potential XSS attacks.Solution
Since you use blade templates you can conditionally use just one form to fulfil both the condifitons: