Laravel未知专栏'updated_at'

时间:2015-02-02 12:38:40

标签: php mysql laravel laravel-migrations

我刚刚开始使用Laravel,我收到以下错误:

  

未知栏'updated_at'插入gebruikers(naam,wachtwoord,   updated_at,created_at)

我知道错误来自迁移表时的时间戳列,但我没有使用updated_at字段。我曾经使用它,当我按照Laravel教程,但现在我正在制作(或试图制作)我自己的东西。即使我不使用时间戳,我也会收到此错误。我似乎无法找到它被使用的地方。这是代码:

控制器

public function created()
{
    if (!User::isValidRegister(Input::all())) {
        return Redirect::back()->withInput()->withErrors(User::$errors);
    }

    // Register the new user or whatever.
    $user = new User;
    $user->naam = Input::get('naam');
    $user->wachtwoord = Hash::make(Input::get('password'));
    $user->save();

    return Redirect::to('/users');
}

路线

Route::get('created', 'UserController@created');

模型

public static $rules_register = [
    'naam' => 'unique:gebruikers,naam'
];

public static $errors;
protected $table = 'gebruikers';

public static function isValidRegister($data)
{
    $validation = Validator::make($data, static::$rules_register);

    if ($validation->passes()) {
        return true;
    }

    static::$errors = $validation->messages();

    return false;
}

我一定是忘了什么......我在这里做错了什么?

4 个答案:

答案 0 :(得分:320)

在模型中,编写以下代码;

public $timestamps = false;

这样可行。

答案 1 :(得分:12)

对于那些使用laravel 5或以上的人必须使用其他方面的公共修饰符,它会抛出异常

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    mapView.delegate = self
    let car = Car(title: "Title", subtitle: "Subtitle", thirdAttribut: "ThirdAttribut", coordinate: CLLocationCoordinate2D(latitude: 50.906849, longitude: 7.524224) )
    mapView.addAnnotation(car)

    }


func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
  //some config stuff
}



    func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
    print("Here Comes the Click")

    print(view.annotation.title)
    print(view.annotation.subtitle)
    //view.annotation.thirdAttribut doesn't existis
    //How get I my "thirdAttribut"-Attribut?

    }

答案 2 :(得分:12)

亚历克斯和萨梅尔的答案很好,但也许只是为什么有必要提供其他信息

public $timestamps = false;

时间戳在官方Laravel page上得到了很好的解释:

  

默认情况下,Eloquent希望您的>表格中存在created_at和updated_at列。如果您不希望由> Eloquent自动管理这些列,请将模型上的$ timestamps属性设置为false。

答案 3 :(得分:4)

将时间戳记设置为false意味着您将同时丢失created_at和Updated_at,而您可以在模型中同时设置两个键。

示例:

情况1: 您有created_at列,但没有update_at,只需在模型中将updated_at设置为false

class ABC extends Model {

const UPDATED_AT = null;

案例2: 您同时拥有created_atupdated_at列,但列名不同 您可以简单地做到:

class ABC extends Model {

const CREATED_AT = 'name_of_created_at_column';
const UPDATED_AT = 'name_of_updated_at_column';

最后完全忽略时间戳

class ABC extends Model {

public $timestamps = false;

希望这会有所帮助 干杯!!!