Reputation: 613
I am trying to store data in a database in Laravel, but the issue is I am able to save the directly using controller.
Now I am using image order change that it working with the jQuery.
After changing orders of the images, I get the json data in which images sources are stored.
Now I want to store the data with image file location(that I will get when I use laravel file storage to convert base64 image src and save it in the disk).
So, only the image src are coming from the josn, the rest I am using Laravel request->input()
for.
Here is the Ajax code:
$(document).ready(function() {
// Initialize sortable
$("#sortable").sortable();
// Save order
$('#submit').click(function() {
var imageids_arr = [];
// get image ids order
$('#sortable li').each(function() {
var id = $(this).data('id');
var json = {
id : id,
src : $(this).data('src')
}
imageids_arr.push(json);
});
console.log(imageids_arr);
var ajaxurl = '/api/add_ref_images_first';
// AJAX request
$.ajax({
url: ajaxurl,
type: 'get',
data: {imageids: imageids_arr},
success: function(response){
if(response == 1)
alert('Save successfully.');
}
});
});
});
Controller code:
public function add_product(Request $request) {
$rules = array(
'name' => 'required',
'author' => 'required',
'short_description' => 'required',
'description' => 'required',
'parent_id' => 'required',
'tags' => 'required',
'price' => 'required',
'sku' => 'required',
'publisher' => 'required',
'thumbnail' => 'required',
'product_images' => 'required',
);
$messages = [
];
$validator = Validator::make($request->all(),$rules, $messages);
if ($validator->fails()) {
return redirect()->back()->withErrors($validator)->withInput();
} else {
$product_id = 'PUBP' . time();
$name = $request->input('name');
$author = $request->input('author');
$short_description = $request->input('short_description');
$long_description = $request->input('description');
$parent_id = $request->parent_id;
$tags = $request->input('tags');
$price = $request->input('price');
$sku = $request->input('sku');
$quantity = $request->input('quantity');
$tax = $request->input('tax');
$selling_price = $request->input('offer_price');
$added_by = Cookie::get('admin_id');
$publisher = $request->input('publisher');
$thumbnail = "";
$thumbnail = $this->thumbnail($request->file('thumbnail'), $product_id);
$product_images = "";
$product_images = $this->product_images($request->file('product_images'), $product_id);
// Note : Here I want the save the image file and get the location
// return $parent_id;
// Saving Data
$result = Product::create([
'product_id' => $product_id,
'name'=>$name,
'author'=>$author,
'slug' => \Str::slug($name),
'short_description'=>$short_description,
'description' => $long_description,
'parent_id' => $parent_id,
'tags' => $tags,
'price' => $price,
'sku' => $sku,
'publisher' => $publisher,
'thumbnail' => $thumbnail,
'images' => $product_images,
'added_by' => $added_by,
'quantity' => $quantity,
'tax' => $tax,
'offer_price' => $selling_price,
'meta_title' => $name,
'meta_description'=> $short_description,
'meta_keywords'=> $tags,
'status' => '0',
])->categories()->sync(explode(',',$parent_id));
if($result){
return redirect('/admin/products')->with( [ 'status' => "Product Uploaded Successfully" ] );
}else{
return redirect('/admin/products')->with( [ 'status' => "Something went wrong" ] );
}
}
}
Ref image store code:
public function product_images($ref_images, $folder_name) {
$fileName = "";
$end_url = "";
$count = 0;
foreach($ref_images as $image)
{
$ref_image_id = 'PUBR'.time().$count++.'.'.$image->getClientOriginalExtension();
$fileName = $image->storeAs($folder_name, $ref_image_id , ['disk' => 'my_uploaded_files']);
if($fileName){
$end_url = $end_url.$fileName.',';
}
}
return $end_url;
}
Json data screenshot:
Upvotes: 0
Views: 822
Reputation: 67
Try This.
use Image;
if ($request->file('product_images')) {
$image = $request->file('product_images');
$ext = explode('/',substr($request->product_images, 0, strpos($request->product_images,';')))[1];
$img->save($end_url);
}
Upvotes: 1