Saving multiple array at once laravel

1

Well, I have a function that is complex, because I need to make multiple for within the same function because of the complexity and the amount of information.

I am consuming an API where I need to collect monthly data of a certain person, and every day can have several items, that is, trading in kids.

Pessoa 01
    MES 01
        PRODUTO 01
        PRODUTO 02
    MES 02
        PRODUTO 01
    MES 03
        PRODUTO 01
        PRODUTO 02
        PRODUTO 03
    MES 04

So I have to go through each die and save one by one. My function developed as follows:

$todosMembros = DB::select('select id from membros');

    foreach ($todosMembros as $membro){
        for($a = 1; $a < 13; $a++){ //AQUI preciso ir pois é mensal então tenho 12 messes
            $categorias = meuAPI; //AQUI preciso ir uma vez para cada membro
            $json_file = file_get_contents($categorias);
            $json_str = json_decode($json_file);
            $todasCategorias = $json_str->list;

            $tamanho = count($todasCategorias);

            if($tamanho == 0){//CASO Não haja dados para ele não faço nada

            }else{
                $tamnhoArray = count($todasCategorias); //QUANTIDADE DE PRODUTOS

                for($w = 0; $w < $tamnhoArray; $w++){ //PARA CADA PRODUTO SALVO NO BANCO
                    DB::select('salvar dados');//AQUI EFETIVO O SALVAMENTO
                }
            }
        }
    }

My problem is that I'm saving but I'm exceeding the browser's wait time, that is, it takes so long in the loops of the function that the timeout is exceeded, I checked the database and it is saving normally, after saving everything I'm ready to check if everything has happened correctly, but nothing appears precisely by the limit.

My question is: What is the best way to get this data into the database, so as not to cause problems for my application

    
asked by anonymous 09.08.2016 / 20:02

1 answer

1

set_time_limit

Usually this happens because of php timeout, tries to set max execution time to a larger value, or zero to disable and not stop until script execution is finished. link

set_time_limit(0)

Sometimes the time out simply because the server did not return any data, in that case it could use the ob_flush() , so the browser will know that it had a response and will not give timeout.

ob_flush ()

As you are displaying content, usually by calling echo , var_dump() , print() , and derivatives, all this will be stored in a buffer, as soon as the buffer reaches the limit size it drops flush() this content to the browser. The ob_flush() serves to "force" this dump, so the content appears on time.

If you put an echo at the end of the last

for($w = 0; $w < $tamnhoArray; $w++){ //PARA CADA PRODUTO SALVO NO BANCO
    DB::select('salvar dados');//AQUI EFETIVO O SALVAMENTO
    echo "Dados foram salvos<br>";
 }

You will notice that it will take some time to get the texts to appear, and when it appears there will appear several lines at a time, this is because everything was in the buffer.

If you add ob_flush() after echo you will see that the text will be displayed line by line.

for($w = 0; $w < $tamnhoArray; $w++){ //PARA CADA PRODUTO SALVO NO BANCO
    DB::select('salvar dados');//AQUI EFETIVO O SALVAMENTO
    echo "Dados foram salvos<br>";
    ob_flush();
 }
What happens is that when I call the echo the text gets buffered, then the ob_flush() forces the dump to the browser, as the only thing in the buffer was the text of the echo it is displayed on the fly.

    
10.08.2016 / 02:17