This is my first question on stack.Here my question is i have function saveMedia, On this function i have save media Images and also set flag is_banner_image or is_thumb_image.When i upload two images and set one image as thumb image it update on table both images as thumb_image. I have added my Controller function here..
public function saveMedia(Request $request) {
$albumId = $request->album_id;
try {
$gallery = Gallery::findOrFail($albumId);
$media = $gallery->media()->findOrNew($request->id);
$media->fill($request->all());
if($request->is_banner_image) { // true or false
$media->setBannerImage();
} else {
$request->is_banner_image = false;
}
if($request->is_thumb_image) { // true or false
$media->setThumbImage();
} else {
$request->is_thumb_image = false;
}
if($media->save()) {
return response()->json([
'id' => $media->id,
'message' => 'Media saved successfully'
]);
} else {
return response()->json([
'message' => 'Unable to save media!!'
]);
}
} catch (\Exception $ex) {
return response()->json([
'message' => 'Unable to save media!!'
]);
}
}
My Model Query function for both thumb image and banner image :
public function setBannerImage() {
try {
DB::table('media')
->whereIn('gallery_id', $this->gallery->source->gallery->modelKeys())
->update(['is_banner_image' => 0]);
$this->is_banner_image = 1;
} catch (\Exception $ex) {
echo $ex->getMessage();
dd($ex->getTraceAsString());
}
}
public function setThumbImage() {
DB::table('media')->
whereIn('gallery_id', $this->gallery->source->gallery->modelKeys())->
update(['is_thumb_image' => 0]);
$this->is_thumb_image = 1;
}
I want to update only one at a time.Thanx in advance.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire