Reputation: 1669
I've got an easy (I guess it's easy) problem. I've created an new model called Player.php. It looks like this:
<?php
class Player extends Eloquent {
protected $table = 'players';
protected $fillable = array('char_name', 'sex');
public $timestamps = false;
}
And I've used this in my controller:
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.');
}
}
This is my form:
@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
And now, this is my error:
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', ))
Also here's a screenshot of my database table 'players':
http://3.imgland.net/lL6EIv.png
Upvotes: 1
Views: 1655
Reputation: 1154
It's likely because you haven't defined relations between a user and a player in your Eloquent model. Here's a common way you could do it (note I'm replacing your "Account" model with "User" [and as such your accounts table would become a users table and the players table would have a foreign key of user_id instead of account_id], as it plays nicer with Laravel's default auth mechanism).
//user.php
class User extends Eloquent {
//Define relation to players
public function players() {
return $this->hasMany('Player');
}
}
//player.php
class Player extends Eloquent {
//Your other player model code
//Define relation to user
public function user() {
return $this->belongsTo('User');
}
}
Now when creating a new player:
$player = new Player();
$player->user_id = Auth::user()->id;
$player->name = $input['char_name'];
$player->sex = $input['sex'];
$player->save();
This assumes the user id is coming from whatever user is logged in.
Upvotes: 0
Reputation: 5353
Well, the error says that you have a constrain on players table. Here you create user:
$player = new User;
$player->name = $input['char_name'];
$player->sex = $input['sex'];
$player->save;
But you didn't set account_id. You may set this field NULL by default in phpmyadmin or delete foreign key constrain or set the right account_id
By the way you can write:
$player = Player::create(array(
'name' => ...
));
instead of creating new object and then saving it
Upvotes: 1
Reputation: 817
You seem to have in your model, a relation between accounts and players. You have to make this relation in your controller.
Upvotes: 0