更新日期,但我收到错误消息:”尝试获取非对象的属性’id'”

huangapple go评论88阅读模式
英文:

Updating date , but i got error message: "Trying to get property 'id' of non-object"

问题

我是laravel 6x中的新手
如果我尝试提交,我会得到"试图获取非对象属性'id'的错误"。错误行在ProudctsController中

  1. public function store(Request $request)
  2. {
  3. // dd($request->all());
  4. DB::beginTransaction();
  5. Try{
  6. $product = Product::create([
  7. "product" => $request->product,
  8. "price" => $request->price,
  9. "stock" => $request->stcok,
  10. "description" => $request->description,
  11. ]);
  12. if($request->hasFile('images') ){
  13. $arrayImages = [];
  14. foreach ($request->images as $value) {
  15. $path = $value->store('product/create');
  16. $columnSImage = [
  17. "product_id" => $product->id,
  18. "image" => $path,
  19. ];
  20. array_push($arrayImages,$columnSImage);
  21. }
  22. ImagesProductDB::insert($arrayImages);
  23. }
  24. DB::commit();
  25. } catch (\Exception $e){
  26. DB::rollback();
  27. dd($e);
  28. }
  29. return redirect()->back();
  30. }
英文:

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

  1. public function store(Request $request)
  2. {
  3. // dd($request->all());
  4. DB::beginTransaction();
  5. Try{
  6. $product = Product::created([
  7. "product" => $request->product,
  8. "price" => $request->price,
  9. "stock" => $request->stcok,
  10. "description" => $request->description,
  11. ]);
  12. if($request->hasFile('images') ){
  13. $arrayImages = [];
  14. foreach ($request->images as $value) {
  15. $path = $value->store('product/create');
  16. $columnSImage = [
  17. "product_id" => $product->id,
  18. "image" => $path,
  19. ];
  20. array_push($arrayImages,$columnSImage);
  21. }
  22. ImagesProductDB::insert($arrayImages);
  23. }
  24. DB::commit();
  25. } catch (\Exception $e){
  26. DB::rollback();
  27. dd($e);
  28. }
  29. return redirect()->back();

答案1

得分: 1

  1. public function store(Request $request)
  2. {
  3. // dd($request->all());
  4. DB::beginTransaction();
  5. Try{
  6. $product = Product::create([
  7. "product" => $request->product,
  8. "price" => $request->price,
  9. "stock" => $request->stock,
  10. "description" => $request->description,
  11. ]);
  12. if($request->hasFile('images') ){
  13. $arrayImages = [];
  14. foreach ($request->images as $value) {
  15. $path = $value->store('product/create');
  16. $columnSImage = [
  17. "product_id" => $product->id,
  18. "image" => $path,
  19. ];
  20. array_push($arrayImages,$columnSImage);
  21. }
  22. ImagesProductDB::insert($arrayImages);
  23. }
  24. DB::commit();
  25. } catch (\Exception $e){
  26. DB::rollback();
  27. dd($e);
  28. }
  29. return redirect()->back();
  30. }
英文:

Try this

created to create

  1. public function store(Request $request)
  2. {
  3. // dd($request->all());
  4. DB::beginTransaction();
  5. Try{
  6. $product = Product::create([
  7. "product" => $request->product,
  8. "price" => $request->price,
  9. "stock" => $request->stcok,
  10. "description" => $request->description,
  11. ]);
  12. if($request->hasFile('images') ){
  13. $arrayImages = [];
  14. foreach ($request->images as $value) {
  15. $path = $value->store('product/create');
  16. $columnSImage = [
  17. "product_id" => $product->id,
  18. "image" => $path,
  19. ];
  20. array_push($arrayImages,$columnSImage);
  21. }
  22. ImagesProductDB::insert($arrayImages);
  23. }
  24. DB::commit();
  25. } catch (\Exception $e){
  26. DB::rollback();
  27. dd($e);
  28. }
  29. return redirect()->back();

huangapple
  • 本文由 发表于 2020年1月6日 17:08:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609308.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定