Improvement of image insertion in bd

0

How would one minimize this image insertion code in the database?

//  controller/GaleriasController.php

public function addGaleria {
  $galeria = new Galeria;
  $galeria->descricao = "galeria 1";
  $galeria->img1 = Input::file('img1');
  $name = Input::file('img1')->getClientOriginalName(); 
  Input::file('img1')->move('/arquivos', $name);

  $galeria->img2 = Input::file('img2');
  $name = Input::file('img2')->getClientOriginalName(); 
  Input::file('img2')->move('/arquivos', $name);

  $galeria->img3 = Input::file('img3');
  $name = Input::file('img3')->getClientOriginalName(); 
  Input::file('img3')->move('/arquivos', $name);
  $galeria->save();
}

You are saving the image in the database and directory correctly, but if I had 10 tables with 10 image fields each ...

I've tried to create an insert array only that I could not: (

    
asked by anonymous 11.02.2014 / 02:33

1 answer

2

My suggestion to improve is this:

//  controller/GaleriasController.php

public function addGaleria
{
  $galeria = new Galeria;
  $galeria->descricao = "galeria 1";
  $max = 3;   // quantidade de imagens

  for ($n = 1; $n <= $max; $n++)
  {
    $img = 'img' . $n;
    $galeria->$img = Input::file($img);
    $name = Input::file($img)->getClientOriginalName(); 
    Input::file($img)->move('/arquivos', $name);
  }

  $galeria->save();
}
    
11.02.2014 / 02:56