I'm so concern about my operation to used Mysql statement like update, delete, insert and within Laravel Eloquent or Query builder because I've create more Mysql statement with more conditional inside of my method in Controller so if one of that conditional or something occurs or meet any problem during I'm processing my method in controller Method I will lost my information or data my data which I want to insert or update to my database.
As below function I used setNotification() which I create in Notification Model and I have method inside that Model which I want to call it to post_data is a method in Controller so if I do so does DB::beginTransaction() will work or not because I prefer to keep all Mysql statement inside all method in Model.
now I currently using Laravel Transaction with Try catch
public function post_data() {
if (Request::ajax() && $this->CheckPermId_from_session(90)) {
$res = null;
$data = Request::except(['_token']);
$rules = [//rules varray];
$data = [//Input data];
$data['tranx_time'] = date("y-m-d H:m:s", time());
$val = Validator::make($data, $rules);
if ($val->fails()) {
$res = ['res' => false, 'form' => false, 'data', $data];
} else {
DB::beginTransaction();
try {
//$update = Teller::where('id', '=', Request::input('teller_till_id'))->update(array('balance' => Request::input('tell_balance')));
$updateTeller = Teller::where('id', '=', Request::input('chief_till_id'))->update(array('balance' => Request::input('last_chief_balance')));
$insertId = DB::table('till_transaction')->insertGetId($data);
if ($insertId && $updateTeller) {
$this->notification->setNotification([$data['to_account'], json_encode($this->group_code), $insertId, Request::input('chief_till_id'), $data['type'], date("Y-m-d H:m:s", time()), $data['type']]);
$res = ['res' => true, 'data', $data];
} else {
$res = ['res' => false, 'data', $data];
}
DB::commit();
} catch (Exception $e) {
DB::rollBack();
throw $e;
}
}
return $res;
}
}
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1SOjcvL
via IFTTT
Aucun commentaire:
Enregistrer un commentaire