/** * * Saves photos with files attached from $file_ids and attaches them to album * * @param AlbumModel $album * @param array $file_ids * @param string[] $file_titles arrray of titles */ protected function savePhotos($album, $file_ids, $file_titles) { $files = File::whereIn('id', $file_ids)->get(); $photos = array(); foreach ($files as $file) { $photo = new PhotoModel(); $photo->title = isset($file_titles[$file->id]) ? $file_titles[$file->id] : ''; $photo->save(); $photo->image()->save($file); $photos[] = $photo; } $album->photos()->saveMany($photos); }