英文:
Updating date , but i got error message: "Trying to get property 'id' of non-object"
问题
我是laravel 6x中的新手
如果我尝试提交,我会得到"试图获取非对象属性'id'的错误"。错误行在ProudctsController中
public function store(Request $request)
{
// dd($request->all());
DB::beginTransaction();
Try{
$product = Product::create([
"product" => $request->product,
"price" => $request->price,
"stock" => $request->stcok,
"description" => $request->description,
]);
if($request->hasFile('images') ){
$arrayImages = [];
foreach ($request->images as $value) {
$path = $value->store('product/create');
$columnSImage = [
"product_id" => $product->id,
"image" => $path,
];
array_push($arrayImages,$columnSImage);
}
ImagesProductDB::insert($arrayImages);
}
DB::commit();
} catch (\Exception $e){
DB::rollback();
dd($e);
}
return redirect()->back();
}
英文:
I'm new in laravel 6x
if i try post , I got "Trying to get property 'id' of non-object" error.And error line in ProudctsController
public function store(Request $request)
{
// dd($request->all());
DB::beginTransaction();
Try{
$product = Product::created([
"product" => $request->product,
"price" => $request->price,
"stock" => $request->stcok,
"description" => $request->description,
]);
if($request->hasFile('images') ){
$arrayImages = [];
foreach ($request->images as $value) {
$path = $value->store('product/create');
$columnSImage = [
"product_id" => $product->id,
"image" => $path,
];
array_push($arrayImages,$columnSImage);
}
ImagesProductDB::insert($arrayImages);
}
DB::commit();
} catch (\Exception $e){
DB::rollback();
dd($e);
}
return redirect()->back();
答案1
得分: 1
public function store(Request $request)
{
// dd($request->all());
DB::beginTransaction();
Try{
$product = Product::create([
"product" => $request->product,
"price" => $request->price,
"stock" => $request->stock,
"description" => $request->description,
]);
if($request->hasFile('images') ){
$arrayImages = [];
foreach ($request->images as $value) {
$path = $value->store('product/create');
$columnSImage = [
"product_id" => $product->id,
"image" => $path,
];
array_push($arrayImages,$columnSImage);
}
ImagesProductDB::insert($arrayImages);
}
DB::commit();
} catch (\Exception $e){
DB::rollback();
dd($e);
}
return redirect()->back();
}
英文:
Try this
created
to create
public function store(Request $request)
{
// dd($request->all());
DB::beginTransaction();
Try{
$product = Product::create([
"product" => $request->product,
"price" => $request->price,
"stock" => $request->stcok,
"description" => $request->description,
]);
if($request->hasFile('images') ){
$arrayImages = [];
foreach ($request->images as $value) {
$path = $value->store('product/create');
$columnSImage = [
"product_id" => $product->id,
"image" => $path,
];
array_push($arrayImages,$columnSImage);
}
ImagesProductDB::insert($arrayImages);
}
DB::commit();
} catch (\Exception $e){
DB::rollback();
dd($e);
}
return redirect()->back();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论