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

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

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();

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:

确定