Home > database >  Attempt to assign property of non-object when updating the existing the record
Attempt to assign property of non-object when updating the existing the record

Time:04-04

I want to update some records from my table and if user has uploaded a picture, I need to upload it and update the seller_certificate_card as well.

Here is my code:

public function doStepTwo(Request $request, $id)
{
    $newSeller = Seller::where('id', $id)->update([
        'seller_first_name' => $request->fname_seller,
        'seller_last_name' => $request->lname_seller,
    ]);
    
    if($request->hasFile('upload_certificate'))
    {
        $destination_path = "public/images/sellers/$user_id";
        $image = $request->file('upload_certificate');
        $image_name = $image->getClientOriginalName();
        $path = $request->file('upload_shenasname')->storeAs($destination_path,$image_name);
        $newSeller->seller_certificate_card = $image_name;
        $newSeller->save();
    }
}

And the seller_certificate_card at the table has this structure:

seller_certificate_card varchar(255)

But I get this error:

Attempt to assign property 'seller_certificate_card' of non-object

So what's going wrong here? How can I fix this issue?

CodePudding user response:

$newSeller = Seller::where('id', $id)->first();
$newSeller->update([
    'seller_first_name' => $request->fname_seller,
    'seller_last_name' => $request->lname_seller,
]);

Try This.

CodePudding user response:

You can get record against ID and then save that record. Use something like this.

$newSeller = Seller::where('id', $id)->first();
$newSeller->seller_first_name = $request->fname_seller;
$newSeller->seller_last_name = $request->lname_seller;
    
    if($request->hasFile('upload_certificate'))
    {
        $destination_path = "public/images/sellers/$user_id";
        $image = $request->file('upload_certificate');
        $image_name = $image->getClientOriginalName();
        $path = $request->file('upload_shenasname')->storeAs($destination_path,$image_name);
        $newSeller->seller_certificate_card = $image_name;
        $newSeller->save(); // or $newSeller->update();
    }
  • Related