大家好,并提前谢谢你,首先对不起,如果我的代码很混乱,我还在学习^^。 我正在尝试制作具有不同属性的产品以及3个不同的图像(制作产品图库)和特征图像, 我成功创建了属性和特征图像。 问题是我无法存储的图库3图像(在DB或图像文件夹中都没有) 我有两个表(product和productgallery),它们之间的关系是hasMany(我正在使用Laravel 5.4)
这是我在运行代码后得到的错误
x as &A
(对不起我的英语,肯定会让我更难理解我的问题)
ProductController
SQLSTATE[HY000]: General error: 1364 Field 'product_id' doesn't have a default value (SQL: insert into `product_galleries` (`updated_at`, `created_at`) values (2017-05-13 20:58:20, 2017-05-13 20:58:20))
上传请求(FormRequest)
public function store(UploadRequest $request)
{
$product = new Product();
$product->product_name = $request->product_name;
$product->product_description = $request->product_description;
if($request->hasFile('product_preview')) {
$file = Input::file('product_preview');
$filename = time(). '-' .$file->getClientOriginalName();
$product->product_preview = $filename;
$file->move(public_path().'/images/product-feature', $filename);
}
$product->category_id = $request->category_id;
$product->color_id = $request->color_id;
$product->size_id = $request->size_id;
$product->material_id = $request->material_id;
// $product->fantasia_id = $request->fantasia_id;
$productgallery = new ProductGallery();
if($request->hasFile('fileToUpload[]')) {
$files = Input::file('fileToUpload[]');
foreach ($request->$files as $photo) {
$filename = time(). '-' .$photo->getClientOriginalName();
$productgallery->product_images = $filename;
$photo->move(public_path().'/images/product-gallery', $filename);
$productgallery->product_id = 1;
}
}
$product->save();
$productgallery->save();
return $this->create()->with('success', 'Uploaded Successfully');
}
表格的视图
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class UploadRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
$rules = [
'product_name' => 'required|max:120',
'category_id' => 'required|integer',
'product_preview' => 'required|image|mimes:jpeg,png,jpg,gif,svg',
];
$fileToUpload = count($this->input('fileToUpload'));
foreach(range(0, $fileToUpload) as $index) {
$rules['fileToUpload.' . $index] = 'image|mimes:jpeg,bmp,png|max:2000';
}
return $rules;
}
}
谢谢
答案 0 :(得分:0)
我认为这就是你想要的。
这将为Product
中传递的每张图片创建一个新的ProductGallery
并创建新的$request->file('fileToUpload[]')
:
$product = new Product();
$product->product_name = $request->product_name;
$product->product_description = $request->product_description;
if($request->hasFile('product_preview')) {
$file = Input::file('product_preview');
$filename = time(). '-' .$file->getClientOriginalName();
$product->product_preview = $filename;
$file->move(public_path().'/images/product-feature', $filename);
}
$product->category_id = $request->category_id;
$product->color_id = $request->color_id;
$product->size_id = $request->size_id;
$product->material_id = $request->material_id;
// $product->fantasia_id = $request->fantasia_id;
// First save the product (so that you have it's ID)
$product->save();
// Then create galleries
if($request->hasFile('fileToUpload[]')) {
$files = Input::file('fileToUpload[]');
foreach ($files as $photo) {
$productgallery = new ProductGallery();
$filename = time(). '-' .$photo->getClientOriginalName();
$productgallery->product_images = $filename;
$photo->move(public_path().'/images/product-gallery', $filename);
$productgallery->product_id = $product->id; // Save it to the newly created product
$productgallery->save();
}
}
return $this->create()->with('success', 'Uploaded Successfully');
答案 1 :(得分:0)
您需要查看Laravel关系API文档。这是一个链接,可以阅读更多相关信息Laravel 5.4 Eloquent Relationships
快速提示修复
您应该在数据库/迁移文件中添加外键
对于迁移,您只需添加
$table->foreign('product_id')->references('id')->on('products');
并将此方法添加到产品模型
public function productgallery(){
return $this->hasMany(Productgallery::class);
}
这是 Productgallery 模型
public function product(){
return $this->belongsTo(Product::class);
}
注意:您仍然需要填写请求中的product_id
字段
套餐解决方案
我建议您使用第三方软件包,因为它使用起来更少,更干净,更坚固。 您可以查看spatie's medialibrary for Laravel