1

I am in need of some assistance.

What I am trying to do in Laravel 5: A cron generated a requisition with multiple line items/products. I have a qty, supplier, part number and name field for each line item created. I can display the line items perfectly. However, I am not sure how it can be updated. What I need is that the qty's can be edited throughout the line items, and once the user clicks save, the controller should update each line item respectively.

What I have so far:

My blade view looks like this:

{!! Form::label('qty', 'Qty', ['class' => 'control-label']) !!}
{!! Form::number("line.$i.qty", $line->qty, ['class' => 'form-control']) !!}

When I do the following in my controller:

$inputs = $request->all();
var_dump($inputs);

I can see the data, but I'm not sure how I can update the DB from there. I'm guessing some type of foreach statement?

Any help will be greatly appreciated.

2 Answers 2

1

You need to resolve which qty values are present and match the values to their products. This can be in several ways, here are two:

A. Encode product id in field name

Do not name your form fields "line.$i.qty" but "line." . $ids[$i] . ".qty".

This will generate field names like line.PID302314.qty, provided you have the product ids stored in the $ids array

Then, when processing the sent values, you scan through all the inputs and if an input matches the name pattern, you extract the product id from the input name and perform the required DB update.

B. Send ids as a hidden value

Add hidden value to your form:

{!! Form::hidden("ids", implode(",", $ids)) !!}

This hidden value defines the order of the product ids with respect to the lines in your form.

Then, when processing the sent values, you explode the ids from the ids:

$ids = explode(",", $request->input('ids'));

and make a for-cycle:

for ($i = 0; $i < count($ids); $i++)
{
    // Update product with id = $ids[$i] with 
    // qty value from input "line." . $i . ".qty"
}
Sign up to request clarification or add additional context in comments.

Comments

0

If i understand you right:

$i=0; // $index
$total_line = 10; // let say.. put the number you need 
while($i <= $total_line ){
    if ($request->has("line.$i.qty")){
        $string = $request->input("line.$i.qty");
        ...
    } 
}

Good luck!

Comments

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.