1

I'm trying to export certain tables in my Laravel project to CSV in order to both back them up and be able to transfer large amounts of data between different instances of the project.

So far, I've got this export function:

private function _export($filename, $table)
{
    array_unshift($table, array_keys($table[0]));

    $handle = fopen($filename, 'w+');
    foreach ($table as $row) {
        fputcsv($handle, $row);
    }
    fclose($handle);
}

This takes in a filename (for example, stages.csv) and an array from a table (for example, Stage::all()->toArray()) and outputs the file.

The project I'm working on has quite a few many-many relationships, each with their own pivot table. What's a good way of getting the rows of the pivot table into an array I can pass to my export function?

Jacob Barrow
  • 631
  • 1
  • 8
  • 25

2 Answers2

1

Use laravel's array_dot() function which will flatten your multidimensional array into a single level with dot notation:

$table = array_dot($table); as the first line of your method.

This assumes you pass the pivot in with the $table variable.

One annoying feature of array_dot() is that empty pivots will be left as an empty array [] rather than an empty string "" (which fputcsv() won't like). So you will want to adjust for that, either using PHP array functions or manually e.g.

foreach ($table as $rowNo => $row) {
    foreach ($row as $col => value) {
        if (is_array($value)) $table[$rowNo][$col] = "";
    }
}
lufc
  • 1,965
  • 2
  • 15
  • 19
  • That's great, cheers! Off the top of your head is there an easy way to then import that pivot csv back into laravel? – Jacob Barrow Dec 15 '18 at 18:58
  • Glad to hear it, please mark the answer as accepted. On your second question, there is no simple way, it would be easier to export the tables separately without joins (one csv per table) if you want to re-import. – lufc Dec 15 '18 at 19:23
0

Converting CSV to PHP Array

You should check your input. Your input here is the same file.

E.g :

<?php
$array=array();
$file=fopen("test.csv","r"));
//check $file has error or not?!
while(($line = fgetcsv($file,1000)) !== false)
{
    $array[]=$line;
}
fclose($file);
?>

OR

<?php
$array = array_map("str_getcsv",preg_split('/\r*\n+|\r+/',file_get_contents("test.csv")));
?>

useful functions :


Useful Links :

Update :

It's better to check the links below. Maybe useful.  

Max Base
  • 639
  • 1
  • 7
  • 15
  • This misses the point question - I've already got a CSV converter, I need to get the pivot table to an array – Jacob Barrow Dec 15 '18 at 17:40
  • Maybe I did not understand what you mean. However, I hope the related links will be useful. @JacobBarrow – Max Base Dec 15 '18 at 18:15
  • If it was not useful ... Explain the issues more clearly. @JacobBarrow – Max Base Dec 15 '18 at 18:15
  • Those links aren't laravel specific, so they don't really apply to the framework I'm using (unless I write raw SQL in my code, which I'm not a fan of) – Jacob Barrow Dec 15 '18 at 18:37