简体   繁体   中英

Preventing multiple value in 3 column in laravel 5.5

I have get problem on this case, I don't know how to prevent multiple value in 3 column, I mean if the 3 column have multiple value it will not insert to database,for example I have Krs table where has data like this :

| id | nim | nip | kode_mk
| 1 | 134 | 154 | 543

and laravel will ignore this data when someone insert data like this :

nim=134,nip=154,kode_mk=543 laravel will not insert and give an atttention

but laravel will accept if data like this :

nim=1132,nip=154,kode_mk=543 laravel will accept and save to database nim=134,nip=1984,kode_mk=543 laravel will accept and save to database nim=1345,nip=154,kode_mk=543 laravel will accept and save to database

I have a table like this where I want prevent the data in the black circle:

在此处输入图片说明

this my Krs.php :

   protected $fillable = ['nim','nip','kode_mk','absen','uts','uas'];
protected $table = 'krs';

public function mahasiswas(){
    return $this->belongsToMany('App\Mahasiswa','id');
}

public function dosens(){
    return $this->belongsToMany('App\Dosen','id');
}

public function makuls(){
    return $this->belongsToMany('App\Matakuliah','id');
}

this my KrsController.php :

public function store(KrsRequest $request)
{

    $krs = new Krs([
        'nim' => $request->get('nim'),
        'nip' => $request->get('nip'),
        'kode_mk' => $request->get('kode_mk'),
        'absen' => $request->get('absen'),
        'uts' => $request->get('uts'),
        'uas' => $request->get('uas')
    ]);

    if ($krs->save()) {
        session()->flash('status','done_all');
        session()->flash('pesan','Data berhasil disimpan');
    }else{
        session()->flash('status','clear');
        session()->flash('pesan','Data gagal disimpan');
    }

    return redirect('Akademik/Krs/create');
}

I don't know how to explain it in english , sorry for my bad grammar

EDIT 1 Table Structure

在此处输入图片说明

In your Krs migration, set the field to require the value to be unique. For example:

$table->string('kode_mk')->unique();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM