나는 쉬운 문제가 있다고 생각한다. Player.php라는 새로운 모델을 만들었습니다.Laravel 4 model
<?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',))
또한 여기 내 데이터베이스의 스크린 샷입니다. 테이블 '선수'
http://3.imgland.net/lL6EIv.png
나는 여전히 사용할 수 있습니다 가정
:.'$ 플레이어 = 새로운 플레이어; $ player-> name = $ input [ 'char_name']; $ player-> sex = $ input [ 'sex']; $ player-> save; 'null로 설정하지 않으려면 어떻게해야 올바른 account_id를 설정할 수 있습니까? 나는이 모든 것에 초보자 일 뿐이며, 당신이 정말로 나에게 깊이 설명해 주길 바란다. – dinomuharemagic
충고 한마디가 아니라 시행 규칙이지만 Laravel의 인증 정보는 "User 모델 이름으로, "사용자"를 테이블 이름으로 사용합니다 (예 : "account"대신). –