php - Laravel update association -
i trying update database entries not work properly, database table want update:
id | product_id | image | image_hq _____________________________________________
in form saving product data, images , images_hq(images higher resolution.
for build model:
namespace app; use illuminate\database\eloquent\model; class images extends model { protected $table = 'image'; protected $fillable = ['product_id', 'name', 'images', 'images_hq']; /** * relations */ public function product() { return $this->belongsto('app\product', 'product_id'); } }
my input fields javascript loop:
<input name="input_image['+i+'][images]" type="file" accept="image/jpeg, image/png"> <input name="input_image['+i+'][images_hq]" type="file" accept="image/jpeg, image/png">
in update method inside of controller tried this:
# update images $sicounter = 0; $imagename = $request->input('input_image'); $imagefile = $request->file('input_image'); if($imagename) { $projectscimgpath = $projectpath . "\\images"; if(!is_dir($projectscimgpath)) { mkdir($projectscimgpath); } $scenesimage = new scenesimages(); foreach ($imagename $imagenamekey => $imagenamevalue) { # sd images if($imagefile[$sicounter]['images'] != null && $imagefile[$sicounter]['images_hq'] == null) { $scimgfile = $imagefile[$sicounter]['images']->getclientoriginalname(); $scenesimage['name'] = $imagenamevalue['name']; $scenesimage['images'] = $projectscimgpath . '\\' . $scimgfile; $scenesimage->product()->associate($product); $scenesimage->save() } # hq images if($imagefile[$sicounter]['images_hq'] != null && $imagefile[$sicounter]['images'] == null) { $scimgfilehq = $imagefile[$sicounter]['images_hq']->getclientoriginalname(); $scimgfilehqext = $imagefile[$sicounter]['images_hq']->getclientoriginalextension(); $scimgfilehq = pathinfo($scimgfilehq, pathinfo_filename) . '_hq' . '.' . $scimgfilehqext; $scenesimage['name'] = $imagenamevalue['name']; $scenesimage['images_hq'] = $projectscimgpath . '\\' . $scimgfilehq; $scenesimage->product()->associate($product); $scenesimage->save() } # if both images if($imagefile[$sicounter]['images_hq'] != null && $imagefile[$sicounter]['images'] != null) { $scimgfile = $imagefile[$sicounter]['images']->getclientoriginalname(); $scimgfilehq = $imagefile[$sicounter]['images_hq']->getclientoriginalname(); $scimgfilehqext = $imagefile[$sicounter]['images_hq']->getclientoriginalextension(); $scimgfilehq = pathinfo($scimgfilehq, pathinfo_filename) . '_hq' . '.' . $scimgfilehqext; $scenesimage['name'] = $imagenamevalue['name']; $scenesimage['images'] = $projectscimgpath . '\\' . $scimgfile; $scenesimage['images_hq'] = $projectscimgpath . '\\' . $scimgfilehq; $scenesimage->product()->associate($product); $scenesimage->save() } $sicounter++; }
it saves data not want saved.
id | product_id | image | image_hq _____________________________________________ 1 | 1 | "path" | "path" _____________________________________________ 2 | 1 | "path" | "" _____________________________________________
at sketch above picked 1 normal , 1 hq image @ first line of input, @ second picked 1 normal image none hq image. correct.
but if want update , add image_hq id 2 following in database:
id | product_id | image | image_hq _____________________________________________ 1 | 1 | "path" | "path" _____________________________________________ 2 | 1 | "path" | "" _____________________________________________ 3 | 1 | "" | "path" _____________________________________________
whereas should this:
id | product_id | image | image_hq _____________________________________________ 1 | 1 | "path" | "path" _____________________________________________ 2 | 1 | "path" | "path" _____________________________________________
how make work?
Comments
Post a Comment