davy_yg's avatar
Level 27

Saving voucher

When saving voucher information for ecommerce I get this message:

500 Server Error

I only modifying what previous dev did. Now, I wonder if this error is customize? Next, why the error appears? The information is saved in the database but the error still appears.

0 likes
5 replies
jlrdw's avatar

The 500 Internal Server Error is a very general HTTP status code that means something has gone wrong on the web site's server but the server could not be more specific on what the exact problem is.

So as suggested to you in the past, use your browser developer tools and look at what's going on.

Try now to remember to always do that as a first step. That's how most of us figure this stuff out.

davy_yg's avatar
Level 27

VoucherController.php

public function store(Request $request, Voucher $voucher)
    {
    $validate = $this->voucher_repo->validateType($request);

    if ($validate) {
        $this->voucher_repo->addAction($request, $voucher);
        Session::flash('flash', 'Successfully add voucher');
        
    } else {
        return redirect()
            ->back()
            ->withError('Persentase Voucher tidak boleh lebih dari sama dengan 100%')->withInput();
    }
    return redirect('voucher');
    }

Voucher Repository.php

public function addAction($request, $voucher)
   {
    $user = json_encode($request['user']);
    $startDate = date('Y-m-d', strtotime($request['start_date']));
    $endDate = date('Y-m-d', strtotime($request['end_date']));

    $attr = [
        "voucher_code" => $request['voucher_code'],
        "discount_type" => $request['discount_type'],
        "discount_value" => $request['discount_value'],
        'minimum_payment' => $request['minimum_payment'],
        'quota' => $request['quota'],
        "start_date" => $startDate,
        "end_date" => $endDate,
        "user" => $user
    ];

    $locales = config('locales');

    foreach ($locales as $key => $value) {
        $voucher->translateOrNew($key)->description = $request->description[$key];
    }

    $voucher->fill($attr);
    $voucher->save();
}

laravel.log

[2019-11-11 11:27:55] development.ERROR: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY' (SQL: insert into voucher_translations (locale, description, voucher_id, updated_at, created_at) values (en, New Discount, 7, 2019-11-11 11:27:54, 2019-11-11 11:27:54)) {"userId":1,"exception":"[object] (Illuminate\Database\QueryException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY' (SQL: insert into voucher_translations (locale, description, voucher_id, updated_at, created_at) values (en, New Discount, 7, 2019-11-11 11:27:54, 2019-11-11 11:27:54)) at C:\xampp\htdocs\sesa\86\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664, Doctrine\DBAL\Driver\PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '0' for key 'PRIMARY' at

This is strange since I do not feel like calling voucher_translations model.

I don't know if there is something to do with the package:

https://packagist.org/packages/dimsav/laravel-translatable

I search the vendor and still do not find the voucher_translation

jlrdw's avatar

But your browser tools will show you exactly the request and responses.

davy_yg's avatar
Level 27

What is browser tools? Inspect Element in Firefox? I currectly use laravel.log to spot the error message.

jove's avatar

Google: "Firefox browser tools" or "Firefox devtools".

Please or to participate in this conversation.