0

I am trying to update data with where condition. I am trying to get the where condition of update statement from the select statement. But in this case the data is not updated.

public function update(string $id, Request $request)
{
    
        $select_id = Finish::select('id')->where('id', $id)->distinct()->first();

        return ($select_id);   //  response === {"id":36}
        
        // this can update data
        Finish::where('id', $id)->update([
            'remarks1' => $request->formData['remarks1']
        ]);
        
        // this can't update data, why???
        Finish::where('id', $select_id)->update([
            'remarks1' => $request->formData['remarks1']
        ]);
        
        return response()->json(['status' => '200']);
    
}

Why is it so?

2
  • 2
    did you read the docs or check the return type of ->first(). This returns a model or null, it does not return a value. You could do this with ->value('id') tho Commented Sep 19 at 16:06
  • What is the point of this function? You get the id passed in un-validated input then load the id of the database record identified by the id ???? Commented Oct 2 at 21:55

1 Answer 1

1

issue is the selected_id variable is an object.

$select_id = Finish::select('id')->where('id', $id)->distinct()->first();

this will return something like this

{"id":36}

So you have to input in the array only the value to the where function.

you could have done it like this

 $select_id = Finish::select('id')->where('id', $id)->distinct()->first();

// Access the property:
Finish::where('id', $select_id->id)->update([
    'remarks1' => $request->formData['remarks1']
]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.