How to update to a collection in the Standard/Eloquent

Asked

Viewed 738 times

0

I am using the Laravel Repository in my project. I created a Service to add some rules. In the upload method it is a reusable method for both saving and updating images. The save method works perfectly with the upload method. The update method does the upload process but does not save the data in the database. Qnd I get the image data in the upload method they arrive as follows:

inserir a descrição da imagem aqui

Dps that they upload and returns the data with the renamed file name returns so: inserir a descrição da imagem aqui

Saving method:

public function save($files, $id)
{
    $arr = $this->doUpload($files);
    foreach($arr as $entry)
    {
      $entry->projects_id = $id->id;
      $entry->save();
    }
    return;
}

Update method that happens nothing:

 public function updateImage($files, $id)
{
     $arr = $this->doUpload($files);
     foreach ($arr as $key) {
      $key = array(
        'filename' => $key->filename,
        'original_filename'=> $key->original_filename,
        'mime'=> $key->mime,
         );
     }
    return $this->uploadsRepository->update($arr, $id);

}

And I tried that too:

 public function updateImage($files, $id)
{
     $arr = $this->doUpload($files);

     foreach ($arr as $key) {
      $this->uploadsRepository->update($key, $id);
     }
    return ;

}

That returns the error:

Argument 1 passed to Prettus Repository Eloquent Baserepository::update() must be of the type array, Object Given, called in C: projetos painel app Services Projectservice.php on line 45 and defined

1 answer

1

I managed to do so:

Uploads::where('id', $id)->update($key);

But I didn’t think it was good practice. I don’t know..

What do you think?

that Uploads:: ?

Browser other questions tagged

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