简体   繁体   中英

Create Data in Laravel-8 Project not working, how to solve it?

I have a problem to add data or create data in my project laravel, how to solve it? i really confused for this

this is the controller

     namespace App\Http\Controllers;
    
    use App\Models\DaftarPelanggan;
    use Illuminate\Http\Request;
    use \Illuminate\Http\Response;
    use Illuminate\Validation\Rule;
    use Illuminate\Support\Facades\Validator;
    use Illuminate\Support\Facades\DB;
    use Alert;
    
    class DaftarPelangganController extends Controller
    {
    public function validator(Request $request)
        {
            return Validator::make($data, [
                'nama_pelanggan' => ['required', 'string', 'max:255'],
                'alamat' => ['required', 'string', 'max:255'],
                'no_telp' => ['required', 'string', 'max:255'],
            ]);
        }
    
        public function create(Request $request)
        {
            return DaftarPelanggan::create([
                'nama_pelanggan' => $data['addNamaPelanggan'],
                'alamat' => $data['addAlamat'],
                'no_telp' => $data['addNoTelp'],
                'email' => $data['addEmail'],
                'poin' => $data['addPOin'],
                'status_member' => $data['addKategori'],
            ]);
            return redirect()->back();
        }
    } 

this is the Routing


Route::get('daftar_pelanggan/create','\App\Http\Controllers\DaftarPelangganController@create')->name('daftar_pelanggan.create');

this is the button blade

    <button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#editModal">
    <i class="fa fa-edit"></i></button>

and this is the modal form


    <!-- Modal  Add -->
          <div class="modal fade" id="addModal" tabindex="-2" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
          <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title mb-0" id="addModalLabel">Tambah Data Pelanggan</h5>
              </div>
              <div class="modal-body">
                <!-- Card body -->
                    <form role="form" action="{{ route('daftar_pelanggan.create') }}" method="POST" enctype="multipart/form-data">
                    @csrf
                 <!-- Input groups with icon -->
                     <div class="form-group row">
                        <label for="addNamaPelanggan" class="col-md-2 col-form-label form-control-label">Nama</label>
                        <div class="col-md-10">
                          <input class="form-control" type="nama" placeholder="Nama Lengkap" id="addNamaPelanggan" name="addNamaPelanggan">
                        </div>
                      </div>
                      <div class="form-group row">
                        <label for="addAlamat" class="col-md-2 col-form-label form-control-label">Alamat</label>
                        <div class="col-md-10">
                          <input class="form-control" type="alamat" placeholder="Jatibarang" id="addAlamat" name="addAlamat">
                        </div>
                      </div>
                      <div class="form-group row">
                        <label for="addNoTelp" class="col-md-2 col-form-label form-control-label">No.Telp</label>
                        <div class="col-md-10">
                          <input class="form-control" type="notelp" placeholder="083XXXXXXXXX" id="addNoTelp" name="addNoTelp">
                        </div>
                      </div>
                      <div class="form-group row">
                        <label for="addEmail" class="col-md-2 col-form-label form-control-label">Email</label>
                        <div class="col-md-10">
                        <input type="email" class="form-control" id="addEmail" placeholder="name@example.com" name="addEmail">
                        </div>
                      </div>
                      <div class="form-group row">
                        <div class="col-md-6">
                          <div class="form-group">
                            <label class="form-control-label" for="addPoin">POIN</label>
                            <input type="text" class="form-control" id="addPoin" placeholder="0" name="addPoin">
                          </div>
                        </div>
                        <div class="col-md-6">
                          <div class="form-group">
                            <label class="form-control-label" for="addKategori">Kategori</label>
                              <select class="form-control" id="addKategori" name="addKategori">
                                <option value="silver">Silver</option>
                                <option value="gold">Gold</option>
                                <option value="diamond">Diamond</option>
                              </select>
                          </div>
                        </div>
                      </div>
                    </form>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary">Tambah Data</button>
              </div>
            </div>
          </div>
        </div>

and this is the model


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class DaftarPelanggan extends Model
{
    //use HasFactory, Notifiable;

    protected $table = "daftar_pelanggans";
    protected $primaryKey = 'id';
    protected $fillable = [
       'nama_pelanggan',
       'alamat',
       'no_telp',
       'email',
       'poin',
       'status_member',
   ];
}

Please help me thank you [modals form my project][1] [1]: https://i.stack.imgur.com/vnSyI.png

In addition to your inputs, your submit button must be inside your form tags.

The button is outside the form tag you should try it inside the form tag

You are using route with method 'get' to access the controller

Route::get('daftar_pelanggan/create','\App\Http\Controllers\DaftarPelangganController@create')->name('daftar_pelanggan.create');

change to 'post' like this

Route::post('daftar_pelanggan/create','\App\Http\Controllers\DaftarPelangganController@create')->name('daftar_pelanggan.create');

I have a problem to add data or create data in my project laravel, how to solve it? i really confused for this

this is the controller

     namespace App\Http\Controllers;
    
    use App\Models\DaftarPelanggan;
    use Illuminate\Http\Request;
    use \Illuminate\Http\Response;
    use Illuminate\Validation\Rule;
    use Illuminate\Support\Facades\Validator;
    use Illuminate\Support\Facades\DB;
    use Alert;
    
    class DaftarPelangganController extends Controller
    {
    public function validator(Request $request)
        {
            return Validator::make($data, [
                'nama_pelanggan' => ['required', 'string', 'max:255'],
                'alamat' => ['required', 'string', 'max:255'],
                'no_telp' => ['required', 'string', 'max:255'],
            ]);
        }
    
        public function create(Request $request)
        {
            return DaftarPelanggan::create([
                'nama_pelanggan' => $data['addNamaPelanggan'],
                'alamat' => $data['addAlamat'],
                'no_telp' => $data['addNoTelp'],
                'email' => $data['addEmail'],
                'poin' => $data['addPOin'],
                'status_member' => $data['addKategori'],
            ]);
            return redirect()->back();
        }
    } 

this is the Routing


Route::get('daftar_pelanggan/create','\App\Http\Controllers\DaftarPelangganController@create')->name('daftar_pelanggan.create');

this is the button blade

    <button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#editModal">
    <i class="fa fa-edit"></i></button>

and this is the modal form


    <!-- Modal  Add -->
          <div class="modal fade" id="addModal" tabindex="-2" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
          <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title mb-0" id="addModalLabel">Tambah Data Pelanggan</h5>
              </div>
              <div class="modal-body">
                <!-- Card body -->
                    <form role="form" action="{{ route('daftar_pelanggan.create') }}" method="POST" enctype="multipart/form-data">
                    @csrf
                 <!-- Input groups with icon -->
                     <div class="form-group row">
                        <label for="addNamaPelanggan" class="col-md-2 col-form-label form-control-label">Nama</label>
                        <div class="col-md-10">
                          <input class="form-control" type="nama" placeholder="Nama Lengkap" id="addNamaPelanggan" name="addNamaPelanggan">
                        </div>
                      </div>
                      <div class="form-group row">
                        <label for="addAlamat" class="col-md-2 col-form-label form-control-label">Alamat</label>
                        <div class="col-md-10">
                          <input class="form-control" type="alamat" placeholder="Jatibarang" id="addAlamat" name="addAlamat">
                        </div>
                      </div>
                      <div class="form-group row">
                        <label for="addNoTelp" class="col-md-2 col-form-label form-control-label">No.Telp</label>
                        <div class="col-md-10">
                          <input class="form-control" type="notelp" placeholder="083XXXXXXXXX" id="addNoTelp" name="addNoTelp">
                        </div>
                      </div>
                      <div class="form-group row">
                        <label for="addEmail" class="col-md-2 col-form-label form-control-label">Email</label>
                        <div class="col-md-10">
                        <input type="email" class="form-control" id="addEmail" placeholder="name@example.com" name="addEmail">
                        </div>
                      </div>
                      <div class="form-group row">
                        <div class="col-md-6">
                          <div class="form-group">
                            <label class="form-control-label" for="addPoin">POIN</label>
                            <input type="text" class="form-control" id="addPoin" placeholder="0" name="addPoin">
                          </div>
                        </div>
                        <div class="col-md-6">
                          <div class="form-group">
                            <label class="form-control-label" for="addKategori">Kategori</label>
                              <select class="form-control" id="addKategori" name="addKategori">
                                <option value="silver">Silver</option>
                                <option value="gold">Gold</option>
                                <option value="diamond">Diamond</option>
                              </select>
                          </div>
                        </div>
                      </div>
                    </form>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary">Tambah Data</button>
              </div>
            </div>
          </div>
        </div>

and this is the model


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class DaftarPelanggan extends Model
{
    //use HasFactory, Notifiable;

    protected $table = "daftar_pelanggans";
    protected $primaryKey = 'id';
    protected $fillable = [
       'nama_pelanggan',
       'alamat',
       'no_telp',
       'email',
       'poin',
       'status_member',
   ];
}

Please help me thank you [modals form my project][1] [1]: https://i.stack.imgur.com/vnSyI.png

I have a problem to add data or create data in my project laravel, how to solve it? i really confused for this

this is the controller

     namespace App\Http\Controllers;
    
    use App\Models\DaftarPelanggan;
    use Illuminate\Http\Request;
    use \Illuminate\Http\Response;
    use Illuminate\Validation\Rule;
    use Illuminate\Support\Facades\Validator;
    use Illuminate\Support\Facades\DB;
    use Alert;
    
    class DaftarPelangganController extends Controller
    {
    public function validator(Request $request)
        {
            return Validator::make($data, [
                'nama_pelanggan' => ['required', 'string', 'max:255'],
                'alamat' => ['required', 'string', 'max:255'],
                'no_telp' => ['required', 'string', 'max:255'],
            ]);
        }
    
        public function create(Request $request)
        {
            return DaftarPelanggan::create([
                'nama_pelanggan' => $data['addNamaPelanggan'],
                'alamat' => $data['addAlamat'],
                'no_telp' => $data['addNoTelp'],
                'email' => $data['addEmail'],
                'poin' => $data['addPOin'],
                'status_member' => $data['addKategori'],
            ]);
            return redirect()->back();
        }
    } 

this is the Routing


Route::get('daftar_pelanggan/create','\App\Http\Controllers\DaftarPelangganController@create')->name('daftar_pelanggan.create');

this is the button blade

    <button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#editModal">
    <i class="fa fa-edit"></i></button>

and this is the modal form


    <!-- Modal  Add -->
          <div class="modal fade" id="addModal" tabindex="-2" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
          <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title mb-0" id="addModalLabel">Tambah Data Pelanggan</h5>
              </div>
              <div class="modal-body">
                <!-- Card body -->
                    <form role="form" action="{{ route('daftar_pelanggan.create') }}" method="POST" enctype="multipart/form-data">
                    @csrf
                 <!-- Input groups with icon -->
                     <div class="form-group row">
                        <label for="addNamaPelanggan" class="col-md-2 col-form-label form-control-label">Nama</label>
                        <div class="col-md-10">
                          <input class="form-control" type="nama" placeholder="Nama Lengkap" id="addNamaPelanggan" name="addNamaPelanggan">
                        </div>
                      </div>
                      <div class="form-group row">
                        <label for="addAlamat" class="col-md-2 col-form-label form-control-label">Alamat</label>
                        <div class="col-md-10">
                          <input class="form-control" type="alamat" placeholder="Jatibarang" id="addAlamat" name="addAlamat">
                        </div>
                      </div>
                      <div class="form-group row">
                        <label for="addNoTelp" class="col-md-2 col-form-label form-control-label">No.Telp</label>
                        <div class="col-md-10">
                          <input class="form-control" type="notelp" placeholder="083XXXXXXXXX" id="addNoTelp" name="addNoTelp">
                        </div>
                      </div>
                      <div class="form-group row">
                        <label for="addEmail" class="col-md-2 col-form-label form-control-label">Email</label>
                        <div class="col-md-10">
                        <input type="email" class="form-control" id="addEmail" placeholder="name@example.com" name="addEmail">
                        </div>
                      </div>
                      <div class="form-group row">
                        <div class="col-md-6">
                          <div class="form-group">
                            <label class="form-control-label" for="addPoin">POIN</label>
                            <input type="text" class="form-control" id="addPoin" placeholder="0" name="addPoin">
                          </div>
                        </div>
                        <div class="col-md-6">
                          <div class="form-group">
                            <label class="form-control-label" for="addKategori">Kategori</label>
                              <select class="form-control" id="addKategori" name="addKategori">
                                <option value="silver">Silver</option>
                                <option value="gold">Gold</option>
                                <option value="diamond">Diamond</option>
                              </select>
                          </div>
                        </div>
                      </div>
                    </form>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary">Tambah Data</button>
              </div>
            </div>
          </div>
        </div>

and this is the model


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class DaftarPelanggan extends Model
{
    //use HasFactory, Notifiable;

    protected $table = "daftar_pelanggans";
    protected $primaryKey = 'id';
    protected $fillable = [
       'nama_pelanggan',
       'alamat',
       'no_telp',
       'email',
       'poin',
       'status_member',
   ];
}

Please help me thank you [modals form my project][1] [1]: https://i.stack.imgur.com/vnSyI.png

I have a problem to add data or create data in my project laravel, how to solve it? i really confused for this

this is the controller

     namespace App\Http\Controllers;
    
    use App\Models\DaftarPelanggan;
    use Illuminate\Http\Request;
    use \Illuminate\Http\Response;
    use Illuminate\Validation\Rule;
    use Illuminate\Support\Facades\Validator;
    use Illuminate\Support\Facades\DB;
    use Alert;
    
    class DaftarPelangganController extends Controller
    {
    public function validator(Request $request)
        {
            return Validator::make($data, [
                'nama_pelanggan' => ['required', 'string', 'max:255'],
                'alamat' => ['required', 'string', 'max:255'],
                'no_telp' => ['required', 'string', 'max:255'],
            ]);
        }
    
        public function create(Request $request)
        {
            return DaftarPelanggan::create([
                'nama_pelanggan' => $data['addNamaPelanggan'],
                'alamat' => $data['addAlamat'],
                'no_telp' => $data['addNoTelp'],
                'email' => $data['addEmail'],
                'poin' => $data['addPOin'],
                'status_member' => $data['addKategori'],
            ]);
            return redirect()->back();
        }
    } 

this is the Routing


Route::get('daftar_pelanggan/create','\App\Http\Controllers\DaftarPelangganController@create')->name('daftar_pelanggan.create');

this is the button blade

    <button type="button" class="btn btn-sm btn-primary" data-toggle="modal" data-target="#editModal">
    <i class="fa fa-edit"></i></button>

and this is the modal form


    <!-- Modal  Add -->
          <div class="modal fade" id="addModal" tabindex="-2" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
          <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title mb-0" id="addModalLabel">Tambah Data Pelanggan</h5>
              </div>
              <div class="modal-body">
                <!-- Card body -->
                    <form role="form" action="{{ route('daftar_pelanggan.create') }}" method="POST" enctype="multipart/form-data">
                    @csrf
                 <!-- Input groups with icon -->
                     <div class="form-group row">
                        <label for="addNamaPelanggan" class="col-md-2 col-form-label form-control-label">Nama</label>
                        <div class="col-md-10">
                          <input class="form-control" type="nama" placeholder="Nama Lengkap" id="addNamaPelanggan" name="addNamaPelanggan">
                        </div>
                      </div>
                      <div class="form-group row">
                        <label for="addAlamat" class="col-md-2 col-form-label form-control-label">Alamat</label>
                        <div class="col-md-10">
                          <input class="form-control" type="alamat" placeholder="Jatibarang" id="addAlamat" name="addAlamat">
                        </div>
                      </div>
                      <div class="form-group row">
                        <label for="addNoTelp" class="col-md-2 col-form-label form-control-label">No.Telp</label>
                        <div class="col-md-10">
                          <input class="form-control" type="notelp" placeholder="083XXXXXXXXX" id="addNoTelp" name="addNoTelp">
                        </div>
                      </div>
                      <div class="form-group row">
                        <label for="addEmail" class="col-md-2 col-form-label form-control-label">Email</label>
                        <div class="col-md-10">
                        <input type="email" class="form-control" id="addEmail" placeholder="name@example.com" name="addEmail">
                        </div>
                      </div>
                      <div class="form-group row">
                        <div class="col-md-6">
                          <div class="form-group">
                            <label class="form-control-label" for="addPoin">POIN</label>
                            <input type="text" class="form-control" id="addPoin" placeholder="0" name="addPoin">
                          </div>
                        </div>
                        <div class="col-md-6">
                          <div class="form-group">
                            <label class="form-control-label" for="addKategori">Kategori</label>
                              <select class="form-control" id="addKategori" name="addKategori">
                                <option value="silver">Silver</option>
                                <option value="gold">Gold</option>
                                <option value="diamond">Diamond</option>
                              </select>
                          </div>
                        </div>
                      </div>
                    </form>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="submit" class="btn btn-primary">Tambah Data</button>
              </div>
            </div>
          </div>
        </div>

and this is the model


<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class DaftarPelanggan extends Model
{
    //use HasFactory, Notifiable;

    protected $table = "daftar_pelanggans";
    protected $primaryKey = 'id';
    protected $fillable = [
       'nama_pelanggan',
       'alamat',
       'no_telp',
       'email',
       'poin',
       'status_member',
   ];
}

Please help me thank you [modals form my project][1] [1]: https://i.stack.imgur.com/vnSyI.png

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