我有一个简单(我想这很容易)的问题。我创建了一个名为 Player.php 的新模型。它看起来像这样:
<?php
class Player extends Eloquent {
protected $table = 'players';
protected $fillable = array('char_name', 'sex');
public $timestamps = false;
}
我已经在我的控制器中使用了它:
public function post_character()
{
$input = Input::all();
$rules = array(
'char_name' => 'required|unique:players,name|min:4',
'sex' => 'required'
);
$validation = Validator::make($input, $rules);
if ($validation->fails()) {
return Redirect::to('create_character')->withErrors($validation);
} else {
$player = new Player;
$player->name = $input['char_name'];
$player->sex = $input['sex'];
$player->save;
return Redirect::to('managment')->with('success', 'Your character has been created.');
}
}
这是我的表格:
@extends('base')
@section('main')
<div class="doc-content-box">
<legend>Create a character</legend>
{{ Form::open(array('url' => 'create_character')) }}
<div class="control-group">
{{ Form::label('char_name', 'Character name') }}
<div class="controls">
{{ Form::text('char_name', null, array('placeholder' => 'Character name goes here..')) }}
</div>
</div>
<div class="control-group">
<div class="controls">
{{ Form::select('sex', array('1' => 'Male', '2' => 'Female'), '1'); }}
</div>
</div>
<div class="form-actions">
{{ Form::submit('Create a character', array('class' => 'btn btn-success')) }}
{{ Form::button('Help', array('class' => 'btn btn-default')) }}
</div>
{{ Form::close() }}
</div>
@endsection
现在,这是我的错误:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`thefs`.`players`, CONSTRAINT `players_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `accounts` (`id`) ON DELETE CASCADE) (SQL: insert into `players` (`name`, `sex`) values (?, ?)) (Bindings: array ( 0 => 'Malx', 1 => '1', ))
另外这里是我的数据库表“玩家”的屏幕截图: