1

I am updating a header amount with each header i am new to laravel please help below is my table, forms, view and controller update function code Table Add Header Name Add Header Amount

View Add Head Name

div class="tab-pane fade show active" id="pills-home" role="tabpanel" aria-labelledby="pills-home-tab">
            <form action="/headers" method="POST">
                @csrf
                <div class="row">
                    <div class="col">
                        <label for="">Header Name</label>
                        <input type="text" name="header_name" required="required">
                        <input type="submit" class="btn btn-primary" name="submit">
                    </div>
                </div>
            </form>
        </div>

View Add Head Amount

<div class="tab-pane fade" id="pills-profile" role="tabpanel" aria-labelledby="pills-profile-tab">
            <form action="/headers" method="POST">
                @csrf
                @foreach ($data as $header)
                    <div class="row">
                        <div class="col">
                            <label for="">Header Name</label>
                            <input type="text" name="header_name[]" value="{{$header->header_name}}" required="required" readonly="readonly">
                            <input type="number" min="0" name="header_amount[]" placeholder="Amount" required="required">
                        </div>
                    </div>
                @endforeach
                <input type="submit" class="btn btn-primary" name="submit1">
            </form>
        </div>

Controller Function

$count = Header::count();
for ($i = 1; $i <= $count; $i++)
            {
                $header = Header::find($i);
                $header->header_amount = $request->input('header_amount');
                $header->save();
            }
            exit();

3 Answers 3

1

Try this code:

$count = Header::count();
$header = Header::find($i);

foreach ($header as $key => $detail) {
   $header->header_amount = $request->input('header_amount')[$key];
   $detail->save();
}

I hope it would be helpful.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks got the logic from your code this works $header = Header::all(); foreach ($header as $key => $detail) { $detail->header_amount = $request->input('header_amount')[$key]; $detail->save(); }
1

I would suggest adding the header ids[] as a hidden input so that you don't have to initially get all of the Header's which will mean one less query.

<div class="row">
    <div class="col">
        <input type="hidden" name="ids[]" value="{{ $header->id }}">
        <label for="">Header Name</label>
        <input type="text" name="header_name[]" value="{{$header->header_name}}" required="required" readonly="readonly">
        <input type="number" min="0" name="header_amount[]" value="{{ $header->header_amount }}" placeholder="Amount" required="required">
    </div>
</div>

Then in your controller you could have something like:

$amounts = request('header_amount');

foreach (request('ids') as $key => $id) {
    Header::where('id', $id)->update(['header_amount' => $amounts[$key]]);
}

1 Comment

will try this great one less query
0

Getting all data from headers table and using foreach loop to update each header row amount

$header = Header::all();
            foreach ($header as $key => $detail)
            {
                $detail->header_amount = $request->input('header_amount')[$key];
                $detail->save();
            }
            exit();

2 Comments

sure i added little explaination
Please explain what you need to change in the original code and why

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.