I am using Maatwebsite\Excel\ExcelServiceProvider::class plugin,I want to store images and product details through Excel sheet.
How can I upload images.excel data?
please help me...
ProductController.php
public function excelupload()
{
if(Input::hasFile('import_file')){
$path = Input::file('import_file')->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
dd($value);
$insert[] = ['product_name' => $value->product_name, 'cataloge_number' => $value->cataloge_number, 'cas_number' => $value->cas_number, 'product_image' => $value->product_image, 'chemical_name' => $value->chemical_name , 'synonyms' => $value->synonyms , 'molecular_formula' => $value->molecular_formula, 'molecular_mass' => $value->molecular_mass ,'product_stock' => $value->product_stock];
}
if(!empty($insert)){
DB::table('products')->insert($insert);
return redirect('admin/product')->with('message', 'New Product Added Successfully!');
}
}
}
return back();
}
If product_image column in excel has URL of image then you need to install Laravel Image Intervention Package: http://image.intervention.io/ or https://github.com/Intervention/image
After install, try this code for store images from URL in Local Laravel Storage and save the path in Database:
public function excelupload()
{
if(Input::hasFile('import_file')){
$path = Input::file('import_file')->getRealPath();
$data = Excel::load($path, function($reader) {
})->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
dd($value);
$file = $value->product_image;
if ($file)
{
$ext = pathinfo($file, PATHINFO_EXTENSION);
$main_image = Image::make(file_get_contents($file))->widen(860, function ($constraint) {
$constraint->upsize();
});
$dir = 'photos/';
$path = uniqid().'.jpg';
$main_image->encode();
Storage::put($dir.$path, (string)$main_image);
$product_image = $path;
}else{
$product_image = null;
}
$insert[] = [
'product_name' => $value->product_name,
'cataloge_number' => $value->cataloge_number,
'cas_number' => $value->cas_number,
// 'product_image' => $value->product_image,
'product_image' => $product_image,
'chemical_name' => $value->chemical_name ,
'synonyms' => $value->synonyms ,
'molecular_formula' => $value->molecular_formula,
'molecular_mass' => $value->molecular_mass,
'product_stock' => $value->product_stock
];
}
if(!empty($insert)){
DB::table('products')->insert($insert);
return redirect('admin/product')->with('message', 'New Product Added Successfully!');
}
}
}
return back();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.