Save data to a variable inside the for in Laravel

Asked

Viewed 63 times

1

I’m trying to save data from a loop inside an array, but when I do var_dump outside the for he just shows me one of the dice someone knows how to fix it ?

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Dados_sd;

class Cadastro_sp1 extends Controller
{
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

       
        for ($i = 0; $i < count($request->allFiles()['arquivo']); $i++ ){
            $arquivo = array([$request->allFiles()['arquivo'][$i]->store('imagens', 'public')]);
            $path =  $arquivo;

        }
        var_dump($path);

    }
}
`

1 answer

-1


Hello,

What’s happening is that you’re overwriting the variable every time. To transform into an array, use the brackets '[]'.

Look below

    path = array(); //inicializando o array (boa prática)
    public function store(Request $request)
    {

       
        for ($i = 0; $i < count($request->allFiles()['arquivo']); $i++ ){
            $arquivo = array([$request->allFiles()['arquivo'][$i]->store('imagens', 'public')]);
            $path[] =  $arquivo; //colocando na proxima posição livre

        }
        var_dump($path);

    }
  • Thank you my brother was this very vlw.

  • Dispose. Good that it worked. If you can accept the answer, so is not pending on the site.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.