Hello,
I am currently working on a Product Create Part, to be associated with an Authenticated User.
Question: When I use one input field and allow multiple upload, how am I supposed to store all files within the database attached to the correct product?
E.g. while creating a product as an Authenticated Business. So, I edit the content such as Title, Brand and Price and want to add 5 pictures to it.
Products table:
public function up()
{
Schema :: create ('products', function(Blueprint $table){
$table->increments('id');
$table->unsignedInteger('business_id');
$table->string ('title')->nullable();
$table->string ('brand')->nullable();
$table->decimal ('price')->nullable();
$table->string('product_image')->default('productdefault.jpg');
$table->timestamps();
});
ProductUploadController.php:
public function postProductEdit(Request $request){
$this->validate($request, [
'title'=> 'max:140',
'brand'=> 'max:140',
'price'=> 'max:10',
'product_image'=> 'max:140',
]);
$update = Auth::user()->products()->create([
'business_id' => auth()->user()->id,
'title' => $request->title,
'brand' => $request->brand,
'price' => $request->price,
'product_image' => $request->product_image,
]);
if($request->hasFile('product_image')){
$business = Auth::user();
$image = $request->file('product_image');
$filename = time() . '.' . $image->getClientOriginalExtension();
$location = public_path('uploads/business/products/'. $filename );
Image::make($image)->resize(812, null, function ($constraint){$constraint->aspectRatio();})->save($location);
$business->product_image = $filename;
$business->save();
return redirect()
->route('products.create')
->with('info', 'Your product has been created.');
}
At the moment, it won't work. Would be happy about your expertise. Thank you