Home » Php » php – Cannot Save Data

php – Cannot Save Data

Posted by: admin February 25, 2020 Leave a comment

Questions:

I was to Try to save any Invoice Card on Database but it not saved

What’s Wrong With my code, I was Try to search to solve this but it not solved

Always Blank to database,

The View Form & Post

The Controller

Use Boostrap 3 from AdminLte Template

<?php

namespace App\Http\Controllers;

use App\Kwitansi;
use Illuminate\Http\Request;
use Haruncpi\LaravelIdGenerator\IdGenerator;

class KwitansiController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $kwitansi = Kwitansi::get();
        return view('user.formkwitansi', ['kwitansi' => $kwitansi]);
    }

    public function store(Request $request)
    {
        $kwitansi = new Kwitansi;

        $id = IdGenerator::generate(['table' => 'kwitansis', 'length' => 10, 'prefix' =>'PM-INV-']);
        $kwitansi->received = $request->received;
        $kwitansi->registration = $request->registration;
        $kwitansi->course = $request->course;
        $kwitansi->trasnportation = $request->trasnportation;
        $kwitansi->payment = $request->payment;
        $kwitansi->kota = $request->kota;
        $kwitansi->register_at = $request->register_at;

      $request->validate([
        'sum_rp' => 'required',
        'sum_text' => 'required',
      ]);

      $sum_rp = $request->registration + $request->course + $request->transportation;

      function penyebut($nilai) {
        $nilai = abs($nilai);
        $huruf = array("", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
        $temp = "";
        if ($nilai < 12) {
            $temp = " ". $huruf[$nilai];
        } else if ($nilai <20) {
            $temp = penyebut($nilai - 10). " belas";
        } else if ($nilai < 100) {
            $temp = penyebut($nilai/10)." puluh". penyebut($nilai % 10);
        } else if ($nilai < 200) {
            $temp = " seratus" . penyebut($nilai - 100);
        } else if ($nilai < 1000) {
            $temp = penyebut($nilai/100) . " ratus" . penyebut($nilai % 100);
        } else if ($nilai < 2000) {
            $temp = " seribu" . penyebut($nilai - 1000);
        } else if ($nilai < 1000000) {
            $temp = penyebut($nilai/1000) . " ribu" . penyebut($nilai % 1000);
        } else if ($nilai < 1000000000) {
            $temp = penyebut($nilai/1000000) . " juta" . penyebut($nilai % 1000000);
        } else if ($nilai < 1000000000000) {
            $temp = penyebut($nilai/1000000000) . " milyar" . penyebut(fmod($nilai,1000000000));
        } else if ($nilai < 1000000000000000) {
            $temp = penyebut($nilai/1000000000000) . " trilyun" . penyebut(fmod($nilai,1000000000000));
        }     
        return $temp;
    }
    function terbilang($nilai) {
        if($nilai<0) {
            $hasil = "minus ". trim(penyebut($nilai));
        } else {
            $hasil = trim(penyebut($nilai));
        }           
        return $hasil;
    }
    $sum_text = $request->sum_rp;

    $kwitansi->sum_rp = $sum_rp;
    $kwitansi->sum_text = terbilang($sum_text);

    $kwitansi->save();

    return redirect('kwitansi')->withSuccess('Added Succesfully');
    }
}

The Blade View

Use Form and View in One Page

@extends('layouts.admin')

@section('title', 'Kwitansi')

@section('css')

<!-- DataTables -->
<link rel="stylesheet" href="{{ asset('adminlte/plugins/datatables-bs4/css/dataTables.bootstrap4.css') }}">
@section('js')
<!-- DataTables -->
<script src="{{ asset('adminlte/plugins/datatables/jquery.dataTables.js') }}"></script>
<script src="{{ asset('adminlte/plugins/datatables-bs4/js/dataTables.bootstrap4.js') }}"></script>
<script>
  $(function () {
    $("#example1").DataTable();
    $('#example2').DataTable({
      "paging": true,
      "lengthChange": false,
      "searching": false,
      "ordering": true,
      "info": true,
      "autoWidth": false,
    });
  });
</script>

@endsection

@section('content')

    <div class="row">
        <div class="col-12">
          <div class="card">
            <div class="card-header">
              <h3 class="card-title">DataTable User</h3>
              @if ($message = Session::get('success'))
              <div class="alert alert-success alert-dismissible">
                  <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                  <h5><i class="icon fas fa-check"></i> Alert!</h5>
                  <p>{{ $message }}</p>
              </div>
              @endif
            </div>
            <!-- /.card-header -->
            <div class="card-body">
                <form action="{{ route('kwitansi.store') }}" method="POST" role="form" class="form-horizontal">
                    @csrf
                    <div class="form-group row">
                        <label for="name" class="control-label col-md-2">Terima Dari</label>
                        <div class="col-md-6">
                            <input type="text" name="received" class="form-control" placeholder="Received Form" />
                        </div>
                    </div> 
                    <div class="form-group row">
                        <label for="registration" class="control-label col-md-2">Biaya Registrasi</label>
                        <div class="col-md-6">
                            <input type="text" name="registration" class="form-control" placeholder="Registration Fee" />
                        </div>
                    </div> 
                    <div class="form-group row">
                        <label for="course" class="control-label col-md-2">Biaya Kursus</label>
                        <div class="col-md-6">
                            <input type="text" name="course" class="form-control" placeholder="Course Fee" />
                        </div>
                    </div> 
                    <div class="form-group row">
                        <label for="trasnportation" class="control-label col-md-2">Biaya Transportasi</label>
                        <div class="col-md-6">
                            <input type="text" name="trasnportation" class="form-control" placeholder="Transportation Fee" />
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="payment" class="control-label col-md-2">Pembayaran Untuk</label>
                        <div class="col-md-6">
                            <input type="text" name="payment" class="form-control" placeholder="Payment Of" />
                        </div>
                    </div> 
                    <div class="form-group row">
                        <label for="register_at" class="control-label col-md-2">Tanggal Registrasi</label>
                        <div class="col-md-6">
                            <input type="text" name="kota"class="form-control" value="Bandung" disabled hidden>
                            <input type="date" name="register_at"class="form-control">
                        </div>
                    </div>
                    <button type="submit" class="btn btn-primary" id="btnsubmit">Tambah Data</button>
                    <button type="reset" class="btn btn-warning" id="btncancel">Cancel</button>
                </form>
              <table id="example2" class="table table-bordered table-hover">
                <thead>
                <tr>
                  <th>No.</th>
                  <th>received</th>
                  <th>registration Fee</th>
                  <th>Course Fee</th>
                  <th>Transportation Fee</th>
                  <th>Grand Total</th>
                  <th>Payment of</th>
                  <th>Action</th>
                </tr>
                </thead>
                <tbody>
                @foreach($kwitansi as $k)
                <tr>
                  <td>{{ $k->id }}</td>
                  <td>{{ $k->received }}</td>
                  <td>{{ $k->registration }}</td>
                  <td>{{ $k->course }}</td>
                  <td>{{ $k->transportation }}</td>
                  <td>{{ $k->sum_rp }}</td>
                  <td>{{ $k->payment }}</td>
                  <td>
                    {{-- <a href="{{ route('user.edit', $u->id)}}" class="btn btn-sm btn-outline-warning"><i class="fas fa-edit"></i></a>
                    <a href="{{ route('user.show', $u->id)}}" class="btn btn-sm btn-outline-info"><i class="fas fa-file"></i></a>
                    <form action="{{ route('user.destroy', $u->id)}}" method="post">
                          @csrf
                          @method('DELETE')
                          <button class="btn btn-sm btn-outline-danger" type="submit"><i class="fas fa-trash-alt"></i></button>
                        </form> --}}
                  </td>
                </tr>
                @endforeach
                </tbody>
                <tfoot>
                <tr>
                    <th>No.</th>
                    <th>received</th>
                    <th>registration Fee</th>
                    <th>Course Fee</th>
                    <th>Transportation Fee</th>
                    <th>Grand Total</th>
                    <th>Payment of</th>
                    <th>Action</th>
                </tr>
                </tfoot>
              </table>
            </div>
            <!-- /.card-body -->
          </div>
          <!-- /.card -->
        </div>
        <!-- /.col -->
      </div>
      <!-- /.row -->

@endsection

I used form and view in one page are this is the problem

This is my model:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Kwitansi extends Model
{
    // $id = IdGenerator::generate(['table' => 'kwitansis', 'length' => 10, 'prefix' =>'PM-INV-']);
    protected $table = 'kwitansis';
    protected $fillable = [
        'received',
        'registration',
        'course',
        'trasnportation',
        'sum_rp',
        'payment',
        'register_at',
    ];
    protected $primarykey = 'id';

    public $increment = false;

    protected $keyType = 'string';
}

How to&Answers:

Remove these lines from your model:

protected $primarykey = 'id';

public $increment = false;

protected $keyType = 'string';

Answer:

Ok Thanks Guys For Helping, I Have Solved The Problem.

I Only Delete Some function on Controller adn Worked for Me

$request->validate([
        'sum_rp' => 'required',
        'sum_text' => 'required',
      ]);

      $sum_rp = $request->registration + $request->course + $request->transportation;

      function penyebut($nilai) {
        $nilai = abs($nilai);
        $huruf = array("", "satu", "dua", "tiga", "empat", "lima", "enam", "tujuh", "delapan", "sembilan", "sepuluh", "sebelas");
        $temp = "";
        if ($nilai < 12) {
            $temp = " ". $huruf[$nilai];
        } else if ($nilai <20) {
            $temp = penyebut($nilai - 10). " belas";
        } else if ($nilai < 100) {
            $temp = penyebut($nilai/10)." puluh". penyebut($nilai % 10);
        } else if ($nilai < 200) {
            $temp = " seratus" . penyebut($nilai - 100);
        } else if ($nilai < 1000) {
            $temp = penyebut($nilai/100) . " ratus" . penyebut($nilai % 100);
        } else if ($nilai < 2000) {
            $temp = " seribu" . penyebut($nilai - 1000);
        } else if ($nilai < 1000000) {
            $temp = penyebut($nilai/1000) . " ribu" . penyebut($nilai % 1000);
        } else if ($nilai < 1000000000) {
            $temp = penyebut($nilai/1000000) . " juta" . penyebut($nilai % 1000000);
        } else if ($nilai < 1000000000000) {
            $temp = penyebut($nilai/1000000000) . " milyar" . penyebut(fmod($nilai,1000000000));
        } else if ($nilai < 1000000000000000) {
            $temp = penyebut($nilai/1000000000000) . " trilyun" . penyebut(fmod($nilai,1000000000000));
        }     
        return $temp;
    }
    function terbilang($nilai) {
        if($nilai<0) {
            $hasil = "minus ". trim(penyebut($nilai));
        } else {
            $hasil = trim(penyebut($nilai));
        }           
        return $hasil;
    }
    $sum_text = $request->sum_rp;

    $kwitansi->sum_rp = $sum_rp;
    $kwitansi->sum_text = terbilang($sum_text);

Only Delete This function and add it to Helper Class