How to update file array in laravel?

1

I am developing a project in which I have to save several files from a table, I can save them the problem is to update or edit I can not do it I get this error

  

"Argument 1 passed to Illuminate \ Database \ Grammar :: columnize () must be   of the type array, integer given, called in   C: \ xampp \ htdocs \ project \ vendor \ laravel \ framework \ src \ Illuminate \ Database \ Query \ Grammars \ Grammar.php   on line 133 "

this the code of my sight

   @foreach($archivos as $archivos)
    <tr>
     <td><input type="text" name="nombre_archivo[]" readonly="" value="{{$archivos->nombre_archivo}}"></td>
     <td><input type="file" name="archivo_unidad[]" value="{{$archivos->archivo_unidad}}"></td>
     <td>
      @if(!isset($archivos->archivo_unidad) || $archivos->archivo_unidad == "NULL")
         <label>Sin archivos</label>
      @else
         <a href='{{ url ('download_archivo',$archivos->archivo_unidad)}}' target="_blank">Descargar</a>
      @endif
     </td>
    </tr>
   @endforeach 

this is my controller

$nombre = $request->input('nombre_archivo');
$file = $request->file('archivo_unidad');
foreach ($request->input('nombre_archivo') as $key => $value) { 
        if(!isset($file[$key])){
           $archivo = DB::table('archivos_unidades')->where('id_unidad', '=', decrypt($id))->value('archivo_unidad');
        }else{
            $archivo = time()."_".$file[$key]->getClientOriginalName();

            \Storage::disk('archivosunidades')->put($archivo,  \File::get($file[$key]));
        }

        $objModel = ArchivosUnidades::find('id_unidad', decrypt($id));
        $objModel->nombre_archivo = $nombre[$key];
        $objModel->id_unidad = $unidades->id_unidad;
        $objModel->archivo_unidad = $archivo;
        $objModel->save();  }

I can not identify what the error is, I stress that in ArchivosUnidades::find('id_unidad', decrypt($id)); id_unidad is not the primary key of the FilesUnits table but the foreign key that is related to my main table called units

View to edit files

View of the database

    
asked by j ch 06.12.2018 в 00:22
source

2 answers

0

I think your eloquent query is missing the "-> get ();"

$archivo = DB::table('archivos_unidades')->where('id_unidad', '=', decrypt($id))->value('archivo_unidad')->get();

and here too:

$objModel = ArchivosUnidades::find('id_unidad', decrypt($id))->get();

I hope that's it. greetings

    
answered by 06.12.2018 в 19:02
0

The find() method allows you to find a model by its primary key, that is its only use, here we see its code:

/**
 * Find a model by its primary key.
 *
 * @param  mixed  $id
 * @param  array  $columns
 * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|static[]|static|null
 */
public function find($id, $columns = ['*'])
{
    if (is_array($id) || $id instanceof Arrayable) {
        return $this->findMany($id, $columns);
    }

    return $this->whereKey($id)->first($columns);
}

In that order of ideas, you are using the wrong method to find a model (taking into account that it is not the primary key you are going to use as a search parameter), I would simply use where:

$objModel = ArchivosUnidades::whereIdUnidad(decrypt($id))->first();
    
answered by 07.12.2018 в 04:01