0

I want to update a row on my table with this query builder.

$new_item = DB::table('peminjams')
            ->where('id', $dipinjam->id_peminjam)
            ->update([
                 'nama' => $request->nama,
                 'cp' => $request->cp,
                 'jaminan' => $request->jaminan
             ]);

when I dd($new_item) it return false, the item did not update.

I still don't know why it failed since its only gives an boolean value.

3
  • try to debug your code. first check ->where('id', $dipinjam->id_peminjam)->first() whether it returns any value. then check the request values. Commented Sep 17, 2021 at 4:12
  • try dd($dipinjam->id_peminjam) can this return any value or not Commented Sep 17, 2021 at 5:04
  • Make sure those attributes of your model are not in $guarded property, because if they are, they won't be mass assignable. Or if you're using $fillable instead, make sure those attributes are in there. Commented Sep 17, 2021 at 6:17

2 Answers 2

1
try {
        // Validate the value...
    } catch (Exception $e) {
        echo $exception->getMessage();exit;

    }

OR

if ($exception instanceof \HttpException || $exception instanceof \ErrorException) {

                $exceptionArry = [
                    'file'    => $exception->getFile(),
                    'code'    => $exception->getCode(),
                    'line'    => $exception->getLine(),
                    'message' => $exception->getMessage(),
                ];
                dd($exceptionArry);
            }

https://laravel.com/docs/5.8/errors

Sign up to request clarification or add additional context in comments.

Comments

1

First, we check first() method to returns value. if return value is null it will return false.

$new_item = DB::table('peminjams')->where('id', $dipinjam->id_peminjam)->first();

if(is_null($new_item)) {
    return false;
} else {
    DB::table('peminjams')
        ->where('id', $dipinjam->id_peminjam)
        ->update([
            'nama' => $request->nama,
            'cp' => $request->cp,
            'jaminan' => $request->jaminan
        ]);
}

using eloquent

$new_item = Peminjams::findOrFail($dipinjam->id_peminjam);

$new_item->nama = $request->nama;
$new_item->cp = $request->cp;
$new_item->jaminan = $request->jaminan;
$new_item->update();

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.