Home > Blockchain >  data was not stored laravel 8 but not getting error
data was not stored laravel 8 but not getting error

Time:01-15

i tried to store data but data not store to database, the field in database and form input already match but still can't store data, and there is no actual message error. please help.

this is my controller:

public function store(Request $request)
{
    $validatedData = $request->validate([
        'kabupaten' => ['required'],
        'provinsi' => ['required'],
        'unit' => ['required'],
        'satuan_kerja' => ['required'],
        'nama_area' => ['required'],
        'kode_area' => ['required']
    ]);
    Area::create($validatedData);

    return redirect('/dashboard/areas')->with('success','Area baru telah ditambahkan!');
}

this is the form input:

<form action="/dashboard/areas" method="POST">
    @csrf
    <div >
        <label for="provinsi" >Provinsi</label>
        <input type="text"  id="provinsi" name="provinsi" value="Jawa Tengah">
    </div>
    <div >
        <label for="kabupaten" >Kabupaten</label>
        <input type="text"  id="kabupaten" name="kabupaten" value="Brebes">
    </div>
    <div >
        <label for="unit" >Unit</label>
        <input type="text"  id="unit" name="unit" value="Pemerintah Kabupaten Brebes">
    </div>
    <div >
        <label for="satuan_kerja" >Satuan Kerja</label>
        <input type="text"  id="satuan_kerja" name="satuan_kerja" value="Pemerintah Desa Dumeling">
    </div>
    <div >
        <label for="nama_area" >Nama Area</label>
        <input type="text"  id="nama_area" name="nama_area">
    </div>
    <div >
        <label for="kode_lokasi" >Kode Lokasi</label>
        <input type="text"  id="kode_lokasi" name="kode_lokasi">
    </div>
    <button type="submit" >Submit</button>
</form>

And this is my area model:

class Area extends Model
{
    use HasFactory;

    protected $primaryKey = 'id_area';
    protected $guarded = [
        'id_area'
    ];

    public function aset(){
        return $this->hasMany(Aset::class, 'id_area');
    }
}

Thank you if there anyone can help me with this problem, I really appreciate it.

CodePudding user response:

So most likely your validation is failing, what you need to do is to display the results of the failed validation error messages, and you can do so in your blade file:

@if ($errors->any())
   @foreach ($errors->all() as $error)
      <div>{{$error}}</div>
   @endforeach
@endif

You may read more on how to display the errors here: https://laravel.com/docs/9.x/validation#quick-displaying-the-validation-errors

You can as well display it per input field or change the class of the input method, etc.. check the @error directive from here: https://laravel.com/docs/9.x/validation#the-at-error-directive

CodePudding user response:

Thanks for helping, I typed the wrong field name in the input form

<div >
     <label for="kode_lokasi" >Kode Lokasi</label>
     <input type="text"  id="kode_area" name="kode_lokasi">
</div>

the name should be: kode_area

  • Related