lundi 11 janvier 2021

Method Illuminate\Database\Query\Builder::id does not exist

I want make auto fill id on add new item. i following tutorial but i get this eror

this is my Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;

class Kode extends Model
{
    protected $table = "report";
    protected $fillable = [
        'id_permintaan', 'tanggal_permintaan', 'tanggal_pengiriman', 'nama_client', 'nama_client', 'jumlah', 'status'
    ];
    public $timestamps = false;
    public $incrementing = false;
    protected $primaryKey = 'id_permintaan';

    public static function kode()
    {
        $id = DB::table('report')->max('id_permintaan');
        $addNol = '';
        $id = str_replace("PM", "", $id);
        $id = (int) $id + 1;
        $incrementid = $id;

        if (strlen($id) == 1) {
            $addNol = "000";
        } elseif (strlen($id) == 2) {
            $addNol = "00";
        } elseif (strlen($id == 3)) {
            $addNol = "0";
        }

        $idBaru = "PM".$addNol.$incrementid;
        return $idBaru;
    }
}

this is my Controller

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Kode;

class ReportController extends Controller
{

    public function tambahreport()
    {
        $id = Kode::id();
        return view('tambahreport');

    }
}

this is my View

<div class="form-group">
ID Permintaan :<input type="text" class="form-control item" name="id_permintaan" required="required" value="" id="id_permintaan"> 
{!! $errors->first('id_permintaan', "<p class='invalid-feedback'>:message</p>") !!}
</div>

if i click add new item i want my id permintaan auto fill like PM1. and if add item again id permintaan is PM2. how to solve this error. thank you



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire