[submit function][1]error message while insertingfunction for inserting tagserror while inserting into database many tags using foreign key .I am getting problem in array , it is unable to insert many tags
public function uploadSubmit(UploadRequest $request)
{
$product = new Product();
$product->name = $request['name'];
$product->save();
//$product = Product::create($request->all());
foreach ($request->photos as $photo) {
$filename = $photo->store('photos');
ProductsPhoto::create([
'product_id' => $product->id,
'filename' => $filename
]);
}
$tags = array();
$tags = array($request['tagname[]']);
foreach ($tags as $tag) {
ProductTag::create([
'tag_id' => $product->id,
'tagname' => $tag,
]);
}
$message = 'Upload successful!';
return redirect('/upload')->with('msg' , $message);
}
[Environment & details:[][2]][sql error 3]
Ok - so after learning a bit more about your current setup I think you would benefit from re-structuring it to the following:
Product
to havetags()
relationship method:taggable
:And here's a test for this setup:
Reference: https://laravel.com/docs/5.6/eloquent-relationships#many-to-many-polymorphic-relations