Laravel雄辩无法将数据提交到数据库

时间:2018-07-12 07:14:59

标签: php laravel eloquent

我正在雄辩地将数据存储在laravel中,我在'/'页面上有表单,该表单在登录前提交数据,并且我希望该数据显示在登录后我在管理页面上创建的表上。问题是,在我单击“提交”按钮后,什么都没有发生,只需返回“ /”页面,表单中的数据甚至都不会存储到数据库中。有什么建议吗,专家?

这是我的代码:

web.php

Route::get('/', 'FrontController@home')->name('home');

Route::get('/login', 'FrontController@login')->name('login');

Route::post('/form', 'FrontController@create')->name('buat');

Auth::routes();

Route::get('/home', 'AdminController@index')->name('admin');

Route::get('/masuk', 'AdminController@masuk')->name('masuk');

Route::get('/keluar', 'AdminController@keluar')->name('keluar');

Route::get('/logout', '\App\Http\Controllers\Auth\LoginController@logout')->name('logout');

Parking.php(雄辩)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Parking extends Model
{

    protected $fillable = ['nopol'];
}

FrontController(访客页面)

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

use App\Parking;

class FrontController extends Controller
{

     /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    public function home () {
        return view ('home');
    }

    public function login () {
        return view ('login');
    }

    public function create(Request $request)
    {


        $request->validate([
            'jenis' => 'in:Motor,Mobil',
            'kategori' => 'in:Umum,Karyawan',
            'nopol' => 'required|string',

        ]);

        $park = Parking::create([
            'jenis' => $request->input('jenis'),
            'kategori' => $request->input('kategori'),
            'nopol' => $request->input('nopol'),
        ]);


        return back()-> with('success', 'Silakan masuk! ID parkir anda '. $park->id .' dengan nomor polisi '. $park->nopol);


    }

}

AdminController(管理页面)

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use DB;

use App\Parking;

class AdminController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin');
    }

    public function masuk(Request $request)
    {
        $park = Parking::all();
        //$hobbies = Hobby::with('user')->get();
        return view('masuk', ['park' => $park]);
    }

     public function keluar()
    {
        return view('keluar');
    }


}

home.blade.php(访客页面)

<!-- start: Form Elements -->
<div class="row-fluid sortable" style="position: absolute; bottom: 30%; left: 30%">
        <div class="box span4">
            <div class="box-header" data-original-title>
                <h2><i class="halflings-icon white edit"></i><span class="break"></span>Input Kendaraan Masuk</h2>
                <div class="box-icon">
                    <a href="#" class="btn-setting"><i class="halflings-icon white wrench"></i></a>
                    <a href="#" class="btn-minimize"><i class="halflings-icon white chevron-up"></i></a>
                    <a href="#" class="btn-close"><i class="halflings-icon white remove"></i></a>
                </div>
            </div>
            <div class="box-content">
                <form class="form-vertical" id="form-masuk" method="POST" action="{{ route('buat') }}">
                    {{ csrf_field() }}
                    <fieldset>

                      <div class="control-group">
                        <label class="control-label" for="typeahead">Jenis Kendaraan</label>
                        <div class="controls">
                          <select id="opt-jenis" name="jenis">
                            <option value="Motor">Motor</option>
                            <option value="Mobil">Mobil</option>
                          </select>
                        </div>
                      </div>

                        <div class="control-group">
                        <label class="control-label" for="typeahead">Kategori Kendaraan</label>
                        <div class="controls">
                          <select id="opt-kategori" name="kategori">
                            <option value="Umum">Umum</option>
                            <option value="Karyawan">Karyawan</option>
                          </select>
                        </div>
                      </div>

                      <div class="control-group">
                            <label class="control-label" for="typeahead">Nomor Polisi</label>
                            <div class="controls">
                              <input class="span6 typeahead" id="nopol" type="text">
                            </div>
                        </div>
                      <div class="form-actions">
                     <button type="submit" form="form-masuk" class="btn btn-primary">Masuk</button>
                        <button type="reset" class="btn" >Batal</button>
                      </div>
                    </fieldset>
                </form>

                </div>
        </div><!--/span-->

    </div><!--/row-->

    </div><!--/.fluid-container-->

masuk.blade.php(显示表格的管理页面)

<div class="row-fluid sortable">        
            <div class="box span12">
                <div class="box-header" data-original-title>
                    <h2><i class="halflings-icon white user"></i><span class="break"></span>Mobil</h2>
                    <div class="box-icon">
                        <a href="#" class="btn-setting"><i class="halflings-icon white wrench"></i></a>
                        <a href="#" class="btn-minimize"><i class="halflings-icon white chevron-up"></i></a>
                        <a href="#" class="btn-close"><i class="halflings-icon white remove"></i></a>
                    </div>
                </div>
                <div class="box-content">
                    <table class="table table-striped table-bordered bootstrap-datatable datatable">
                      <thead>
                          <tr>
                              <th>ID</th>
                              <th>Nomor Polisi</th>
                              <th>Waktu Masuk</th>
                              <th>Jenis Kendaraan</th>
                              <th>Kategori Kendaraan</th>
                              <th>Status</th>
                          </tr>
                      </thead>   
                      <tbody>
                        @foreach ($park as $p)
                        <tr>
                            <td>{{ $p->id }}</td>
                            <td>{{ $p->nopol }}</td>
                            <td class="center">{{ $p->created_at }}</td>
                            <td class="center">{{ $p->jenis }}</td>
                            <td class="center">{{ $p->kategori }}</td>
                            <td class="center">
                                <a class="label label-info" href="#">Masuk</a>
                            </td>
                        </tr>
                        @endforeach

                    </tbody>
                  </table>            
                </div>
            </div><!--/span-->

        </div><!--/row-->

1 个答案:

答案 0 :(得分:1)

您只允许批量分配nopol

// in your Parking model
protected $fillable = ['nopol'];

// this won't work
$park = Parking::create([
    'jenis' => $request->input('jenis'),
    'kategori' => $request->input('kategori'),
    'nopol' => $request->input('nopol'),
]);

// change your parking model fillable to this
protected $fillable = ['jenis', 'kategori', 'nopol'];

或者,在$guarded模型上设置Parking属性:

// allow all fields except id
protected $guarded = ['id'];

或者允许所有内容:

protected $fillable = ['*'];

最后,使用$request方法可发挥自己的优势:

$park = Parking::create($request->only(['jenis',kategori','nopol']));

Mass Assignment