How to insert data to database with Laravel












0














I'm trying to insert my data to database from form.



My URL to create the data is web.com/siswa/create



But when I click submit system show error MethodNotAllowedHttpException.



How I can fix it? Is there anything wrong with my code?



Here is my form:



<form action="{{ url('siswa') }}" method="POST">
<div class="form-group">
<label for="exampleInputEmail1">NISN</label>
<input type="text" class="form-control" name="nisn" id="nisn" placeholder="NISN"></div>
<div class="form-group">
<label for="exampleInputEmail1">Nama Siswa</label>
<input type="text" class="form-control" name="nama_siswa" id="nama_siswa" placeholder="Nama Siswa"> </div>
<button type="submit" class="btn btn-success btn-sm font-weight-bold">Submit</button></form>


Controller:



public function tambah()
{
return view('siswa.create');
}

public function store(Request $request)
{

$siswa = new AppSiswa;
$siswa->nisn = $request->nisn;
$siswa->nama_siswa = $request->nama_siswa;
$siswa->tanggal_lahir = $request->tanggal_lahir;
$siswa->jenis_kelamin = $request->jenis_kelamin;
$siswa->save();
return redirect('siswa');
}


Route:



Route::get('/siswa/create', [
'uses' => 'SiswaController@tambah',
'as' => 'tambah_siswa'
]);

Route::get('/siswa', [
'uses' => 'SiswaController@store',
'as' => 'simpan_siswa'
]);









share|improve this question




















  • 2




    You only have get routes but your form is trying to POST
    – Phil
    Nov 12 at 4:44










  • i change it but now system show me error 419 Sorry, your session has expired. Please refresh and try again.
    – Jazuly
    Nov 12 at 4:47
















0














I'm trying to insert my data to database from form.



My URL to create the data is web.com/siswa/create



But when I click submit system show error MethodNotAllowedHttpException.



How I can fix it? Is there anything wrong with my code?



Here is my form:



<form action="{{ url('siswa') }}" method="POST">
<div class="form-group">
<label for="exampleInputEmail1">NISN</label>
<input type="text" class="form-control" name="nisn" id="nisn" placeholder="NISN"></div>
<div class="form-group">
<label for="exampleInputEmail1">Nama Siswa</label>
<input type="text" class="form-control" name="nama_siswa" id="nama_siswa" placeholder="Nama Siswa"> </div>
<button type="submit" class="btn btn-success btn-sm font-weight-bold">Submit</button></form>


Controller:



public function tambah()
{
return view('siswa.create');
}

public function store(Request $request)
{

$siswa = new AppSiswa;
$siswa->nisn = $request->nisn;
$siswa->nama_siswa = $request->nama_siswa;
$siswa->tanggal_lahir = $request->tanggal_lahir;
$siswa->jenis_kelamin = $request->jenis_kelamin;
$siswa->save();
return redirect('siswa');
}


Route:



Route::get('/siswa/create', [
'uses' => 'SiswaController@tambah',
'as' => 'tambah_siswa'
]);

Route::get('/siswa', [
'uses' => 'SiswaController@store',
'as' => 'simpan_siswa'
]);









share|improve this question




















  • 2




    You only have get routes but your form is trying to POST
    – Phil
    Nov 12 at 4:44










  • i change it but now system show me error 419 Sorry, your session has expired. Please refresh and try again.
    – Jazuly
    Nov 12 at 4:47














0












0








0







I'm trying to insert my data to database from form.



My URL to create the data is web.com/siswa/create



But when I click submit system show error MethodNotAllowedHttpException.



How I can fix it? Is there anything wrong with my code?



Here is my form:



<form action="{{ url('siswa') }}" method="POST">
<div class="form-group">
<label for="exampleInputEmail1">NISN</label>
<input type="text" class="form-control" name="nisn" id="nisn" placeholder="NISN"></div>
<div class="form-group">
<label for="exampleInputEmail1">Nama Siswa</label>
<input type="text" class="form-control" name="nama_siswa" id="nama_siswa" placeholder="Nama Siswa"> </div>
<button type="submit" class="btn btn-success btn-sm font-weight-bold">Submit</button></form>


Controller:



public function tambah()
{
return view('siswa.create');
}

public function store(Request $request)
{

$siswa = new AppSiswa;
$siswa->nisn = $request->nisn;
$siswa->nama_siswa = $request->nama_siswa;
$siswa->tanggal_lahir = $request->tanggal_lahir;
$siswa->jenis_kelamin = $request->jenis_kelamin;
$siswa->save();
return redirect('siswa');
}


Route:



Route::get('/siswa/create', [
'uses' => 'SiswaController@tambah',
'as' => 'tambah_siswa'
]);

Route::get('/siswa', [
'uses' => 'SiswaController@store',
'as' => 'simpan_siswa'
]);









share|improve this question















I'm trying to insert my data to database from form.



My URL to create the data is web.com/siswa/create



But when I click submit system show error MethodNotAllowedHttpException.



How I can fix it? Is there anything wrong with my code?



Here is my form:



<form action="{{ url('siswa') }}" method="POST">
<div class="form-group">
<label for="exampleInputEmail1">NISN</label>
<input type="text" class="form-control" name="nisn" id="nisn" placeholder="NISN"></div>
<div class="form-group">
<label for="exampleInputEmail1">Nama Siswa</label>
<input type="text" class="form-control" name="nama_siswa" id="nama_siswa" placeholder="Nama Siswa"> </div>
<button type="submit" class="btn btn-success btn-sm font-weight-bold">Submit</button></form>


Controller:



public function tambah()
{
return view('siswa.create');
}

public function store(Request $request)
{

$siswa = new AppSiswa;
$siswa->nisn = $request->nisn;
$siswa->nama_siswa = $request->nama_siswa;
$siswa->tanggal_lahir = $request->tanggal_lahir;
$siswa->jenis_kelamin = $request->jenis_kelamin;
$siswa->save();
return redirect('siswa');
}


Route:



Route::get('/siswa/create', [
'uses' => 'SiswaController@tambah',
'as' => 'tambah_siswa'
]);

Route::get('/siswa', [
'uses' => 'SiswaController@store',
'as' => 'simpan_siswa'
]);






php laravel laravel-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 7:28









Mukyuu

6071419




6071419










asked Nov 12 at 4:43









Jazuly

17211




17211








  • 2




    You only have get routes but your form is trying to POST
    – Phil
    Nov 12 at 4:44










  • i change it but now system show me error 419 Sorry, your session has expired. Please refresh and try again.
    – Jazuly
    Nov 12 at 4:47














  • 2




    You only have get routes but your form is trying to POST
    – Phil
    Nov 12 at 4:44










  • i change it but now system show me error 419 Sorry, your session has expired. Please refresh and try again.
    – Jazuly
    Nov 12 at 4:47








2




2




You only have get routes but your form is trying to POST
– Phil
Nov 12 at 4:44




You only have get routes but your form is trying to POST
– Phil
Nov 12 at 4:44












i change it but now system show me error 419 Sorry, your session has expired. Please refresh and try again.
– Jazuly
Nov 12 at 4:47




i change it but now system show me error 419 Sorry, your session has expired. Please refresh and try again.
– Jazuly
Nov 12 at 4:47












5 Answers
5






active

oldest

votes


















4














change your store function route from get to post



Route::post('/siswa', [
'uses' => 'SiswaController@store',
'as' => 'simpan_siswa'
]);


Use Csrf protection field in your form for the session timeout error



{{ csrf_field() }}


OR



<input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />


OR if you are using Form builder



{!! Form::token() !!}





share|improve this answer























  • i change it but now system show me error 419 Sorry, your session has expired. Please refresh and try again.
    – Jazuly
    Nov 12 at 4:47










  • you need to refresh the form and try again as csrf protection will not allow to refresh the form itself from clicking f5
    – Leena Patel
    Nov 12 at 4:48






  • 1




    yup fix it with add '{{ csrf_field() }}' in the form.
    – Jazuly
    Nov 12 at 4:49



















3














In Route please use post instead of get



Route::post('/siswa','SiswaController@store');


and also include {{ csrf_field() }} in form






share|improve this answer































    0














    you are using method="POST" on your form but in on your route you are using Route::get



    Use Route::post for your route






    share|improve this answer





























      0














      In your form you've given POST method, but your router doesn't have any POST handler. So all you have to do is , when you are trying to store data from form to DB you have to post the data, and the router should handle it.



      Try this



      Route::post('/siswa', [
      'uses' => 'SiswaController@store',
      'as' => 'simpan_siswa'
      ]);





      share|improve this answer





























        0














        You are using POST method in your form and using GET in route.



        try this



        Route::post( '/siswa', [
        'uses' => 'SiswaController@store',
        'as' => 'simpan_siswa'
        ] );





        share|improve this answer





















          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256075%2fhow-to-insert-data-to-database-with-laravel%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          change your store function route from get to post



          Route::post('/siswa', [
          'uses' => 'SiswaController@store',
          'as' => 'simpan_siswa'
          ]);


          Use Csrf protection field in your form for the session timeout error



          {{ csrf_field() }}


          OR



          <input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />


          OR if you are using Form builder



          {!! Form::token() !!}





          share|improve this answer























          • i change it but now system show me error 419 Sorry, your session has expired. Please refresh and try again.
            – Jazuly
            Nov 12 at 4:47










          • you need to refresh the form and try again as csrf protection will not allow to refresh the form itself from clicking f5
            – Leena Patel
            Nov 12 at 4:48






          • 1




            yup fix it with add '{{ csrf_field() }}' in the form.
            – Jazuly
            Nov 12 at 4:49
















          4














          change your store function route from get to post



          Route::post('/siswa', [
          'uses' => 'SiswaController@store',
          'as' => 'simpan_siswa'
          ]);


          Use Csrf protection field in your form for the session timeout error



          {{ csrf_field() }}


          OR



          <input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />


          OR if you are using Form builder



          {!! Form::token() !!}





          share|improve this answer























          • i change it but now system show me error 419 Sorry, your session has expired. Please refresh and try again.
            – Jazuly
            Nov 12 at 4:47










          • you need to refresh the form and try again as csrf protection will not allow to refresh the form itself from clicking f5
            – Leena Patel
            Nov 12 at 4:48






          • 1




            yup fix it with add '{{ csrf_field() }}' in the form.
            – Jazuly
            Nov 12 at 4:49














          4












          4








          4






          change your store function route from get to post



          Route::post('/siswa', [
          'uses' => 'SiswaController@store',
          'as' => 'simpan_siswa'
          ]);


          Use Csrf protection field in your form for the session timeout error



          {{ csrf_field() }}


          OR



          <input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />


          OR if you are using Form builder



          {!! Form::token() !!}





          share|improve this answer














          change your store function route from get to post



          Route::post('/siswa', [
          'uses' => 'SiswaController@store',
          'as' => 'simpan_siswa'
          ]);


          Use Csrf protection field in your form for the session timeout error



          {{ csrf_field() }}


          OR



          <input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />


          OR if you are using Form builder



          {!! Form::token() !!}






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 12 at 5:05

























          answered Nov 12 at 4:47









          Leena Patel

          1,6151620




          1,6151620












          • i change it but now system show me error 419 Sorry, your session has expired. Please refresh and try again.
            – Jazuly
            Nov 12 at 4:47










          • you need to refresh the form and try again as csrf protection will not allow to refresh the form itself from clicking f5
            – Leena Patel
            Nov 12 at 4:48






          • 1




            yup fix it with add '{{ csrf_field() }}' in the form.
            – Jazuly
            Nov 12 at 4:49


















          • i change it but now system show me error 419 Sorry, your session has expired. Please refresh and try again.
            – Jazuly
            Nov 12 at 4:47










          • you need to refresh the form and try again as csrf protection will not allow to refresh the form itself from clicking f5
            – Leena Patel
            Nov 12 at 4:48






          • 1




            yup fix it with add '{{ csrf_field() }}' in the form.
            – Jazuly
            Nov 12 at 4:49
















          i change it but now system show me error 419 Sorry, your session has expired. Please refresh and try again.
          – Jazuly
          Nov 12 at 4:47




          i change it but now system show me error 419 Sorry, your session has expired. Please refresh and try again.
          – Jazuly
          Nov 12 at 4:47












          you need to refresh the form and try again as csrf protection will not allow to refresh the form itself from clicking f5
          – Leena Patel
          Nov 12 at 4:48




          you need to refresh the form and try again as csrf protection will not allow to refresh the form itself from clicking f5
          – Leena Patel
          Nov 12 at 4:48




          1




          1




          yup fix it with add '{{ csrf_field() }}' in the form.
          – Jazuly
          Nov 12 at 4:49




          yup fix it with add '{{ csrf_field() }}' in the form.
          – Jazuly
          Nov 12 at 4:49













          3














          In Route please use post instead of get



          Route::post('/siswa','SiswaController@store');


          and also include {{ csrf_field() }} in form






          share|improve this answer




























            3














            In Route please use post instead of get



            Route::post('/siswa','SiswaController@store');


            and also include {{ csrf_field() }} in form






            share|improve this answer


























              3












              3








              3






              In Route please use post instead of get



              Route::post('/siswa','SiswaController@store');


              and also include {{ csrf_field() }} in form






              share|improve this answer














              In Route please use post instead of get



              Route::post('/siswa','SiswaController@store');


              and also include {{ csrf_field() }} in form







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 12 at 5:55









              Udhav Sarvaiya

              1,20461322




              1,20461322










              answered Nov 12 at 4:49









              raushan kumar

              6910




              6910























                  0














                  you are using method="POST" on your form but in on your route you are using Route::get



                  Use Route::post for your route






                  share|improve this answer


























                    0














                    you are using method="POST" on your form but in on your route you are using Route::get



                    Use Route::post for your route






                    share|improve this answer
























                      0












                      0








                      0






                      you are using method="POST" on your form but in on your route you are using Route::get



                      Use Route::post for your route






                      share|improve this answer












                      you are using method="POST" on your form but in on your route you are using Route::get



                      Use Route::post for your route







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 12 at 4:50









                      Paul Mikki

                      583




                      583























                          0














                          In your form you've given POST method, but your router doesn't have any POST handler. So all you have to do is , when you are trying to store data from form to DB you have to post the data, and the router should handle it.



                          Try this



                          Route::post('/siswa', [
                          'uses' => 'SiswaController@store',
                          'as' => 'simpan_siswa'
                          ]);





                          share|improve this answer


























                            0














                            In your form you've given POST method, but your router doesn't have any POST handler. So all you have to do is , when you are trying to store data from form to DB you have to post the data, and the router should handle it.



                            Try this



                            Route::post('/siswa', [
                            'uses' => 'SiswaController@store',
                            'as' => 'simpan_siswa'
                            ]);





                            share|improve this answer
























                              0












                              0








                              0






                              In your form you've given POST method, but your router doesn't have any POST handler. So all you have to do is , when you are trying to store data from form to DB you have to post the data, and the router should handle it.



                              Try this



                              Route::post('/siswa', [
                              'uses' => 'SiswaController@store',
                              'as' => 'simpan_siswa'
                              ]);





                              share|improve this answer












                              In your form you've given POST method, but your router doesn't have any POST handler. So all you have to do is , when you are trying to store data from form to DB you have to post the data, and the router should handle it.



                              Try this



                              Route::post('/siswa', [
                              'uses' => 'SiswaController@store',
                              'as' => 'simpan_siswa'
                              ]);






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 12 at 4:50









                              Sathiya Seelan D

                              237




                              237























                                  0














                                  You are using POST method in your form and using GET in route.



                                  try this



                                  Route::post( '/siswa', [
                                  'uses' => 'SiswaController@store',
                                  'as' => 'simpan_siswa'
                                  ] );





                                  share|improve this answer


























                                    0














                                    You are using POST method in your form and using GET in route.



                                    try this



                                    Route::post( '/siswa', [
                                    'uses' => 'SiswaController@store',
                                    'as' => 'simpan_siswa'
                                    ] );





                                    share|improve this answer
























                                      0












                                      0








                                      0






                                      You are using POST method in your form and using GET in route.



                                      try this



                                      Route::post( '/siswa', [
                                      'uses' => 'SiswaController@store',
                                      'as' => 'simpan_siswa'
                                      ] );





                                      share|improve this answer












                                      You are using POST method in your form and using GET in route.



                                      try this



                                      Route::post( '/siswa', [
                                      'uses' => 'SiswaController@store',
                                      'as' => 'simpan_siswa'
                                      ] );






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 12 at 6:54









                                      Arif Hossain

                                      12




                                      12






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256075%2fhow-to-insert-data-to-database-with-laravel%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Florida Star v. B. J. F.

                                          Error while running script in elastic search , gateway timeout

                                          Adding quotations to stringified JSON object values