1

I have a file I am uploading and I am passing an partid along with it. I need to insert the records from the csv file into the table along with the partid in each record.

Inserting the records from the file works fine.

I need to add the partid to the array. I have not been able to figure out how. Any suggestions would be appreciated.

Submit Form

<form style="border: 4px solid #a1a1a1;margin-top: 15px;padding: 10px;"
      action="/part/importbom" class="form-horizontal" method="post"
      enctype="multipart/form-data">

    {{ csrf_field() }}
    <input type="file" name="file"/>
    <input type="hidden" name="part_id" value="{{ $part->id }}"/>
    <button class="btn btn-primary">Import File</button>
</form>

Controller

public function importExcel(Request $request)
{
    $partid = $request->part_id;

    $validator = \Validator::make($request->all(), [
        'file' => 'required',
    ]);

    if ($validator->fails()) {
        return redirect()->back()->withErrors($validator);
    }

    $file = $request->file('file');
    $csvData = file_get_contents($file);

    $rows = array_map('str_getcsv', explode("\n", $csvData));
    $header = array_shift($rows);

    foreach ($rows as $row) {
        $row = array_combine($header, $row);
        // dd($row);
        Bom::create([
            'item'             => $row['item'],
            'qty'              => $row['qty'],
            'designators'      => $row['designators'],
            'hand_add'         => $row['hand_add'],
            'hand_solder'      => $row['hand_solder'],
            'hand_solder_pins' => $row['hand_solder_pins'],
            'notes'            => $row['notes'],
            'install'          => $row['install'],
        ]);
    }

    return redirect()->back();
}
3
  • Why don't you use $request to get part id Commented Jun 20, 2018 at 15:14
  • I can get the part id, how do I add it to the array so it is inserted with each record? Commented Jun 20, 2018 at 15:19
  • What version of Laravel are you using? Commented Jun 20, 2018 at 18:09

2 Answers 2

2

You should just be able to include it in the foreach loop:

foreach ($rows as $row) {

    $row = array_combine($header, $row);

    Bom::create([
        'part_id'          => $request->part_id,
        'item'             => $row['item'],
        'qty'              => $row['qty'],
        'designators'      => $row['designators'],
        'hand_add'         => $row['hand_add'],
        'hand_solder'      => $row['hand_solder'],
        'hand_solder_pins' => $row['hand_solder_pins'],
        'notes'            => $row['notes'],
        'install'          => $row['install'],
    ]);
}

Or to make your code shorter you could do:

foreach ($rows as $row) {

    $row = array_combine($header, $row);

    $row['part_id'] = $request->part_id;

    Bom::create($row);
}
Sign up to request clarification or add additional context in comments.

5 Comments

@Subtlebot I'm not sure I follow, the above code isn't creating any new CSV. It's simply taking each row from a CSV file and adding that row to a new row in the db. The OP mentioned that the adding of the CSV data is working fine and that they just weren't sure of how to add the part_id into the array.
I tried both of your suggestions and still get the ErrorException (E_WARNING) array_combine(): Both parameters should have an equal number of elements
@user2325548 In that case can you add the csv data from the file you're uploading to your question. The reason you would get that sort of error is because one of the rows has a different number of columns to the header row.
Well that was it. I did not have data yet for the fields past designators. I added the blank commas for the respective null fields and it worked. ugh... Thanks so much!
@user2325548 Glad I could help!
1

Regarding Laravel doc: https://laravel.com/docs/5.6/requests

public function store(Request $request)
{
    $partId = $request->input('part_id');

    //
}

Then you pass your variable $partId where ever you need it.

4 Comments

In the controller code above, i need to get $partid in the Bom::create array. As I mentioned I can ready get $partid, i just don't know how to include it in the create array that is parsing the csv file and inserting the record.
for example, and i know this is wrong, but what i am trying accomplish Bom::create([ 'part_id' => $partid, 'item' => $row['item'],
ok I go your point, so what is wrong with the method in your comment, do you get error? does the key part_id created in your model?
ErrorException (E_WARNING) array_combine(): Both parameters should have an equal number of elements

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.