Okay I have been struggling with this for like 3 hours trying different things and I cannot get it to work at all here is my function:
public function endshiftdata_post() {
Session::forget('success');
Session::forget('error');
$serial = Input::get('hidden_serial');
$game = Game::find($serial);
$shiftsgame = ShiftsGames::find($serial);
$sold = $game->tickets_sold();
$cash = Input::get('cash');
$unsold = Input::get('unsold');
$redeemed = Input::get('redeemed');
$input = Input::all();
$rules = array(
'cash' => 'required|numeric',
'unsold' => 'required|numeric',
'redeemed' => 'required|numeric',
);
$validation = Validator::make($input, $rules);
if ($shiftsgame->cash_out == 0) {
if ($validation->passes()) {
$shiftsgame->cash_out = $cash;
$game->tickets_unsold = $unsold;
$game->tickets_sold = $sold;
$shiftsgame->prizes_redeemed = $redeemed;
$game->prizes_redeemed = $redeemed;
$shiftsgame->end_time = Shifts::currentTime();
$game->end_shift_data = 1;
$game->save();
$shiftsgame->save();
Session::flash('success', "Successfully submitted data for " . $game->name . " (" . $game->serial . ")");
return Redirect::route('shift.index');
}
else {
Session::flash('error', "Make sure text boxes contain numbers only.");
return Redirect::route('shift.index');
}
}
}
if I datadump the object it shows this here is the important info on it: array(9) { ["id"]=> int(40) ["user_id"]=> int(41) ["site_id"]=> int(1) ["serial"]=> string(8) "6743591 " ["start_time"]=> string(19) "2014-08-29 09:58:42" ["end_time"]=> string(19) "2014-08-29 09:59:39" ["cash_in"]=> int(10) ["cash_out"]=> int(500) ["prizes_redeemed"]=> int(500) } ["original":protected]=> array(9) { ["id"]=> int(40) ["user_id"]=> int(41) ["site_id"]=> int(1) ["serial"]=> string(8) "6743591 " ["start_time"]=> string(19) "2014-08-29 09:58:42" ["end_time"]=> string(19) "2014-08-29 09:59:39" ["cash_in"]=> int(10) ["cash_out"]=> int(500) ["prizes_redeemed"]=> int(500)
So as you can see the cash_out is 500 for two of them.
However, in my database I have 4 rows total...
And it does contain a 0 but obviously this datadump shows those are not getting included for some reason even though the serial number is the same which is what the ShiftGames::find($serial) should do...
Does anyone have any ideas what is going on? I am so stuck :(:(:( pleaseee help!
Edit for pierre:
Okay so here is my ShiftGames class:
class ShiftsGames extends Eloquent {
protected $table = 'shiftsgames';
protected $primaryKey = 'serial';
/**
*
*
* The attributes excluded from the model's JSON form.
*
*
*
* @var array
*
*/
protected $hidden = array();
public $timestamps = false;
public function getStartTime() {
return $this->start;
}
public function getEndTime() {
return $this->end;
}
public static function currentTime() {
date_default_timezone_set('America/Chicago');
$now = new DateTime;
return $now->format('Y-m-d g:i:s');
}
public function scopeCurrent($query) {
$query->where('user_id', '=', Auth::user()->id);
$query->where('end_time', '=', null);
return $query;
}
public static function endShift() {
$query = Shifts::current()->first();
$query->end_time = Shifts::currentTime();
$query->save();
}
}
What I did to fix it was switch the protected $primaryKey = 'serial'; to protected $primaryKey = 'id';
I than switched my function to have this:
Session::forget('success');
Session::forget('error');
$serial = Input::get('hidden_serial');
$game = Game::find($serial);
$sold = $game->tickets_sold();
$cash = Input::get('cash');
$unsold = Input::get('unsold');
$redeemed = Input::get('redeemed');
$input = Input::all();
$rules = array(
'cash' => 'required|numeric',
'unsold' => 'required|numeric',
'redeemed' => 'required|numeric',
);
$validation = Validator::make($input, $rules);
foreach (ShiftsGames::get() as $shiftgame) {
if ($shiftgame->cash_out == 0 and $serial == $shiftgame->serial) {
if ($validation->passes()) {
$shiftgame->cash_out = $cash;
$game->tickets_unsold = $unsold;
$game->tickets_sold = $sold;
$shiftgame->prizes_redeemed = $redeemed;
$game->prizes_redeemed = $redeemed;
$shiftgame->end_time = Shifts::currentTime();
$game->end_shift_data = 1;
$game->save();
$shiftgame->save();
Session::flash('success', "Successfully submitted data for " . $game->name . " (" . $game->serial . ")");
return Redirect::route('shift.index');
}
else {
Session::flash('error', "Make sure text boxes contain numbers only.");
return Redirect::route('shift.index');
}
}
}
}
}
Pierre does this look like an acceptable fix to you? or do you see problems with what I am doing still? It seems to be working.