Heril Muratovic
Heril Muratovic

Reputation: 2028

Laravel - CSV - getting only one row as response

I have one CSV file with two fields field1 and field2. When I try to loop throught each row of my CSV file it only return first row of file.

Notice, that file is uploaded and valid.

Here is my code:

/**
 * Get File Contents
 */
 $csv = Reader::createFromPath(storage_path('folder/'.$FILE_NAME));
 $csv->setOffset(1);

/**
 * CSV Properties
 */
 $delimiter = $csv->getDelimiter();
 $enclosure = $csv->getEnclosure();

 /**
  * Processing CSV File
  */
 $temp_data = "";
 $csv->each(function($row) use ($position1, $position2, $delimiter, &$temp_data, $enclosure)
 {
    $temp_data .= $enclosure.$row[$position1].$enclosure.$delimiter.
                    $enclosure.$row[$position2].$enclosure."/n";

    echo $temp_data;
 });

 exit;

Does anyone have an idea why I'm getting only the first row of the file?

Upvotes: 0

Views: 933

Answers (1)

KoldBane
KoldBane

Reputation: 217

Using the CSV plugin by The League (csv.thephpleague.com), the "each" method must return true on each iteration, or else it will stop running. This will cause the above method to only run 1 iteration before terminating, resulting in a single line output.

Upvotes: 1

Related Questions