Laravel 5.5在提交时重复插入数据库

时间:2017-09-01 14:01:01

标签: php mysqli laravel-5

有人可以帮助我...当我点击提交时,我的laravel代码插入两次数据库,它将双数据存储到数据库, 这是我的代码: 控制器

public function store(Request $request)
{   
    $this ->validate($request,[
        'nim' => 'required|max:8',
        'nama' => 'required|max:30',
        'alamat' => 'required|max:100',
        'jenis_kelamin' => 'required|max:9',
        'no_tlp' => 'required|regex:/[0-9]{12}/',
        'tempat' => 'required',
        'tanggal' => 'required',
        'id_jurusan' => 'required'
    ]);
    $simpan = new Mahasiswa([
        'nim' => $request->get('nim'),
        'nama' => $request->get('nama'),
        'alamat' => $request->get('alamat'),
        'jenis_kelamin' => $request->get('jenis_kelamin'),
        'no_tlp' => $request->get('no_tlp'),
        'tempat' => $request->get('tempat'),
        'tanggal' => $request->get('tanggal'),
        'id_jurusan' => $request->get('id_jurusan')
    ]);
    $simpan->save();

    if (Mahasiswa::create($request->all())) {
        $request->session()->flash('status', 'success');
        $request->session()->flash('pesan', 'Data Berhasil Disimpan');
    }else{
        $request->session()->flash('status', 'danger');
        $request->session()->flash('pesan', 'Data gagal Disimpan!!');
    }

    return redirect('/Mahasiswa/create');
}

这是我的型号代码:

class Mahasiswa extends Model
{
    protected $fillable = ['nim','nama','alamat','jenis_kelamin','no_tlp','tempat','tanggal','id_jurusan'];
    protected $table = 'mahasiswa';
}

提前致谢

1 个答案:

答案 0 :(得分:3)

您要保存两次数据:

add_shortcode( 'show_cart_items', 'tcf_show_cart_items' );
function tcf_show_cart_items()
{
    $cart   =   '<table><tr>';
                foreach(  WC()->cart->get_cart() as $cart_item )
                    $cart .= '<td>' . $cart_item['data']->get_title() . '</td>';
    $cart   .=  '</tr></table>';

    return $cart;
}

然后再次:

$simpan->save();

只需删除Mahasiswa::create($request->all()) 行。