Search code examples
laraveldatabaselaravel-8laravel-livewire

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'namapenerima' cannot be null


I'm using a Livewire component, why is it still showing "cannot be null" even though all fields in the form have been filled out? I want the data that I fill in the form to be inserted into the database.

Bayar View

            <form wire:submit.prevent="store">
                @csrf
              
                <div class="form-group">
                  <label for="namapenerima">Nama Lengkap :</label>
                  <input type="text" name="namapenerima" class="form-control" value="{{ Auth::user()->name }}" placeholder="Masukkan nama lengkap Anda" required>
                </div>
              
                <div class="form-group">
                  <label for="email">Email:</label>
                  <input type="email" name="email" class="form-control" value="{{ Auth::user()->email }}" placeholder="Masukkan alamat email Anda" required>
                </div>
              
                <div class="form-group">
                  <label for="phone">Nomor Telepon:</label>
                  <input type="number" name="phone" class="form-control" value="{{ Auth::user()->phonenumber }}" placeholder="Masukkan nomor telepon Anda" required>
                </div>
              
                <div class="form-group" style="margin-top: 20px">
                  <label for="alamat">Alamat Lengkap:</label>
                  <textarea name="alamat" class="form-control" placeholder="Masukkan alamat lengkap Anda" required></textarea>
                </div>
              
                <button type="submit" class="submit-pesan" style="margin-top: 20px">Pesan Sekarang</button>
              </form>

Bayar Component

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\Order;
use App\Models\Alamat;
use Illuminate\Support\Facades\Auth;

class Bayar extends Component
{
    public $snapToken;
    public $belanja;
    public $va_number, $gross_amount, $bank, $transaction_status, $deadline;
    public $namapenerima, $email, $phone, $alamat;

    public function mount($id)
    {
        if (!Auth::user()) {
            return redirect()->route('login');
        }

        // Set your Merchant Server Key
        \Midtrans\Config::$serverKey = 'SB-Mid-server-d8dGSfkiYcOsQ5Kqs8NCTNrs';
        // Set to Development/Sandbox Environment (default). Set to true for Production Environment (accept real transaction).
        \Midtrans\Config::$isProduction = false;
        // Set sanitization on (default)
        \Midtrans\Config::$isSanitized = true;
        // Set 3DS transaction for credit card to true
        \Midtrans\Config::$is3ds = true;

        if (isset($_GET['result_data'])) {

            $current_status         = json_decode($_GET['result_data'], true);
            $order_id               = $current_status['order_id'];
            $this->belanja          = Order::where('id', $order_id)->first();
            $this->belanja->status  = 2;
            $this->belanja->update();
        } else {
            //ambil data belanja
            $this->belanja = Order::find($id);
        }

        if (!empty($this->belanja)) {
            if ($this->belanja->status == 1) {
                $params = array(
                    'transaction_details' => array(
                        'order_id' => $this->belanja->id,
                        'gross_amount' => $this->belanja->total_harga,
                    ),
                    'customer_details' => array(
                        'first_name' => 'Tuan',
                        'last_name' => Auth::user()->name,
                        'email' => Auth::user()->email,
                        'phone' => Auth::user()->phonenumber,
                    ),
                );

                $this->snapToken = \Midtrans\Snap::getSnapToken($params);
            } else if ($this->belanja->status == 2) {
                $status = \Midtrans\Transaction::status($this->belanja->id);
                $status = json_decode(json_encode($status), true);
                //menampilkan status pembayaran
                $this->va_number            = $status['va_numbers'][0]['va_number'];
                $this->gross_amount         = $status['gross_amount'];
                $this->bank                 = $status['va_numbers'][0]['bank'];
                $this->transaction_status   = $status['transaction_status'];
                $transaction_time           = $status['transaction_time'];
                $this->deadline             = date('Y-m-d H:i:s', strtotime('+1 day', strtotime($transaction_time)));
            }
        }
    }

    public function store ()
    {
        Alamat::create([
            'namapenerima' => $this->namapenerima,
            'email' => $this->email,
            'phone' => $this->phone,
            'alamat' => $this->alamat,
        ]);
    }

    public function render()
    {
        return view('livewire.bayar')
            ->extends('layouts.product-layouts')->section('product');
    }
}

Models

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Alamat extends Model
{
    use HasFactory;
    protected $table = 'alamat';
    protected $fillable = [
        'namapenerima',
        'email',
        'phone',
        'alamat',
    ];
}

Database Schema

    public function up()
    {
        Schema::create('alamat', function (Blueprint $table) {
            $table->id();
            $table->string('namapenerima');
            $table->string('email');
            $table->string('phone');
            $table->string('alamat');
            $table->timestamps();
        });
    }

I want the data that I fill in the form to be inserted into the database.


Solution

  • You need to use wire:model to "bind" (or "synchronize") the current value of some HTML element with a specific property in livewire component, such as:

    <input type="text" wire:model.defer="namapenerima" name="namapenerima" class="form-control" value="{{ Auth::user()->name }}" placeholder="Masukkan nama lengkap Anda" required>
    

    Reference: https://laravel-livewire.com/docs/2.x/properties#data-binding