@Snapey @primordial
Ok here is my complete store method, I am having trouble with 2 areas any help would be appreciated.
- pricing stores the same data in all fields
- album is not storing fields only the file
public function store(CreateProductRequest $request)
{
// /**
// * Validate the submitted Data
// */
$this->validate($request, [
'name' => 'required',
// 'manufacturer' => 'required',
// 'price' => 'required',
// 'details' => 'required',
// 'quantity' => 'required',
// 'categories' => 'required',
// 'thumbnail' => 'required|image',
// ]);
if($request->hasFile('album')){
foreach($request->album as $photo){
if($photo && strpos($photo->getMimeType(), 'image') === false){
return \Redirect()->back();
}
}
}
/**
* Upload a new thumbnail and thumbnail2
*/
$dest = 'uploads/products/today/';
$name = str_random(11) . '_' . $request->file('thumbnail')->getClientOriginalName();
$request->file('thumbnail')->move($dest, $name);
$name2 = str_random(11) . '_' . $request->file('thumbnail2')->getClientOriginalName();
$request->file('thumbnail2')->move($dest, $name2);
//$product = $request->all();
$input = $request->all();
//dd($input);
$input['thumbnail'] = '/' . $dest . $name;
$input['thumbnail2'] = '/' . $dest . $name2;
$product = Product::create($input, $request->except(
'attribute_name',
'product_attribute_value',
'price',
'sku',
'quantity',
'model',
'upc',
'alt_details',
'feature_name',
'use_icon',
'icon',
'photo_src',
'alt',
'caption',
'photoinfo',
'linkto',
'use_main',
'use_thumb',
'use_gallery'
));
if(!empty($request->price)){
foreach($request->price as $productPrice){
$price = new Price();
$price->product_id = $product->id;
$price->price = $productPrice;
$price->upc = $productPrice;
$price->model = $productPrice;
$price->quantity = $productPrice;
$price->sku = $productPrice;
$price->alt_details = $productPrice;
$product->prices()->save($price);
//dd($price);
}
}
// if($request->has('productPrices')){
// foreach($request->productPrices as $price){
// if(!empty($price['price'])){
// $product->prices()->create($price);
// }
// }
// }
/**
* Upload Album Photos
*/
if($request->hasFile('album')){
foreach($request->album as $photo){
if($photo){
$name = str_random(11) . "_" . $photo->getClientOriginalName();
$photo->move($dest, $name);
AlbumPhoto::create([
'product_id' => $product->id,
'photo_src' => "/" . $dest . $name,
'alt' => $photo->alt,
'caption' => $photo->caption,
'photoinfo' => $photo->photoinfo,
'linkto' => $photo->linkto,
'use_main' => $photo->use_main,
'use_thumb' => $photo->use_thumb,
'use_gallery' => $photo->use_gallery
]);
}
}
}
/**
* Linking the categories to the product
*/
foreach($request->categories as $category_id){
CategoryProduct::create(['category_id' => $category_id, 'product_id' => $product->id]);
}
/**
* Linking the options to the product
*/
if($request->has('options')){
foreach($request->options as $option_details){
if(!empty($option_details['name']) && !empty($option_details['values'][0])){
$option = Option::create([
'name' => $option_details['name'],
'product_id' => $product->id
]);
foreach($option_details['values'] as $value){
OptionValue::create([
'value' => $value,
'option_id' => $option->id
]);
}
}
}
}
if(!empty($request->attribute_name)){
foreach($request->attribute_name as $key => $item){
$productVariant = new ProductVariant();
$productVariant->attribute_name = $item;
$productVariant->product_attribute_value = $request->product_attribute_value[$key];
$product->productVariants()->save($productVariant);
}
}
if(!empty($request->feature_name)){
foreach($request->feature_name as $feature){
$productFeature = new ProductFeature();
$productFeature->feature_name = $feature;
$product->productFeatures()->save($productFeature);
}
}
FlashAlert()->success('Success!', 'The Product Was Successfully Added');
return \Redirect(getLang() . '/admin/products');
}
I added except the product create command for all the fields that belong to other tables but not really sure what the function of this is for but thought it was worth a try.