无法保存数据

Cannot Save Data

我试图在数据库中保存任何发票卡,但它没有保存

我的代码出了什么问题,我试图搜索解决这个问题但没有解决

数据库始终为空,

The View Form & Post

控制器

使用 AdminLte 模板中的 Boostrap 3

<?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');
    }
}

Blade 视图

在一页中使用表单和视图

@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

我在一个页面中使用表单和视图是这个问题

这是我的模型:

<?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';
}

从您的模型中删除这些行:

protected $primarykey = 'id';

public $increment = false;

protected $keyType = 'string';

好的,谢谢大家的帮助,我已经解决了问题。

我只删除了控制器上的一些功能并且对我有用

$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);

仅删除此功能并将其添加到Helper Class