簡體   English   中英

laravel 4.2 - 不使用遷移的數據庫

[英]laravel 4.2 - database without using migrations

我通過運行SQL查詢創建了一個數據庫。

mysql查詢人員表如下。

use highwaymate;

create table staff(
  staff_ID int auto_increment primary key,
  first_name varchar(50) not null,
  last_name varchar(50) not null,
  title enum('miss','mrs','mr','ms') not null,
  address varchar(500) not null,
  date_of_birth date,
  email varchar(50),
  telephone varchar(10),
  role enum('academic','non academic'),
  designation_ID int,
  contract_type enum('temporary','permanent'), 
  etf_number varchar(15),
  NIC varchar(10) not null,
  date_started date not null,
  resigned_date date,
  timestamp datetime default now(),
  constraint fk_staff foreign key(designation_ID) references designations(designation_ID) on update cascade
);

我的員工模型如下

<?php

class Staff extends Eloquent
{
  protected $table = 'staff';
  protected $primaryKey='staff_ID';
  protected $timepstamps=false;
  public $incrementing=false;

}

我的路線如下

Route::group(array('before'=>'csrf'),function(){
  Route::post('/category/index/staff/new',array('uses'=>'CategoryController@postStaffAdd', 'as' => 'category-postStaffAdd'));
});

我的員工控制員如下

<?php

class CategoryController extends BaseController {

    public function index(){

      return View::make('categories.index');
    }

    public function staffIndex()
    {
      $staff = Staff::all();
      return View::make('categories.staffpage')->with('staff',$staff);
    }

    public function staffAdd(){
      return View::make('categories.staffadd');
    }

    public function postStaffAdd(){

      $staff = new Staff;

      $staff->first_name = 'John';

      $staff->last_name = 'Dalton';

      $staff->title = 'mr';

      $staff->address = 'Panagoda, Hoamgama';

      $staff->date_of_birth = '1990-12-21';

      $staff->email = 'johnnydalton@gmail.com';

      $staff->telephone = '0775698521';

      $staff->role = 'academic';

      $staff->designation_ID = 3;

      $staff->contract_type = 'permanent';

      $staff->etf_number = 'pending';

      $staff->NIC = '901254586V';

      $staff->date_started = '2015-05-02';

      $staff->save();

      return "You have entered a new staff member successfully";
    }



}

我的問題是postStaffAdd()方法,它不起作用。

以下是我發現的錯誤=>

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: insert into `staff` (`first_name`, `last_name`, `title`, `address`, `date_of_birth`, `email`, `telephone`, `role`, `designation_ID`, `contract_type`, `etf_number`, `NIC`, `date_started`, `updated_at`, `created_at`) values (John, Dalton, mr, Panagoda, Hoamgama, 1990-12-21, johnnydalton@gmail.com, 0775698521, academic, 3, permanent, pending, 901254586V, 2015-05-02, 2016-02-05 09:59:57, 2016-02-05 09:59:57))

Illuminate \ Database \ QueryException

我覺得; 數據類型存在問題,因為當我看到參數時,字符串字段(如first_name似乎是非字符串。 那是問題嗎?

幫我解決我的問題。

提前致謝。

您拼錯了用於禁用Staff模型中的時間戳的屬性名稱,並且該屬性必須是public不受protected而不是:

protected $timepstamps = false;
//             ^
// There's an extra "p" in the property name

它應該是:

public $timestamps = false;

Laravel Eloquent文檔中所示

由於您沒有使用遷移,因此您的表沒有laravel添加的默認timestamps (即created_at和updated_at列)。

您有兩種方法可以解決這個問題:

  • 將列添加到表中(最好通過遷移
  • 或者告訴你的模型你不會使用時間戳,就像這樣

protected $timestamps = false;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM