dongshou9343
2017-09-01 14:01
浏览 168
已采纳

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

can someone help me...my laravel code insert twice to database when I click submit, it store double data to database, here is my code : controller

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

and this my model code :

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

Thanks in advance

图片转代码服务由CSDN问答提供 功能建议

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

 公共函数存储(请求$请求)
 {
 $ this 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',\  n'tanggal'=>'required',
'id_jurusan'=>'required'
]); 
 $ simpan = new Mahasiswa([
'nim'=> $ request-> get  ('nim'),
'nama'=> $ request-> get('nama'),
'alamat'=> $ request-> get('alamat'),
'enjenis_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())){\  n $ request-> session() - > flash('status','success'); 
 $ request-> session() - > flash('pesan','Data Berhasil Disimpan'); \  n} else {
 $ request-> session() - > flash('status','danger'); 
 $ request-> session() - > flash('pesan','Data gagal  Disimpan !!'); 
} 
 
返回redirect('/ Mahasiswa / create'); 
} 
   
 
 

这是我的模型代码:

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

提前致谢

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douxu5845 2017-09-01 14:05
    已采纳

    You are saving your data twice:

    $simpan->save();
    

    and then again:

    Mahasiswa::create($request->all())
    

    Just remove the $simpan->save(); line.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题