Laravel update() returns true but not update the data

Viewed 50

here I'd like to create and update data in a single request. The problem is, the data cannot changed while the update() returns true. Here's my $supply->create():

    public function create($attributes){
    try {
        $this->repository->create($attributes); // works fine

        # update product's stock
        $product = $this->product_repository->find($attributes['product_id']);  // works fine
        $product_attributes = [
            'stocks' => $product->stocks += $attributes['quantity'],
        ];

        # public function update($id, array $attributes); as my interface method
        $this->product_repository->update($product->id, $product_attributes); // this is return true but changes nothing.
        
        return response()->json([
            'success' => true,
            'message' => 'a new record has been created.',
            'data' => $attributes
        ], 201);
    } catch (\Exception $e) {
        return response()->json([
            'success' => false,
            'message' => $e->getMessage(),
        ]);
    }
}

And this is my $product_repository->update():

public function update($id, array $attributes)
{
    return $this->product->findOrFail($id)->update($attributes);  
}

Should I to use observer here?

0 Answers
Related