Last Updated: February 25, 2016
·
5.92K
· dearteno

Eloquent - Transaction

/* 
 * Saving Question
 */
$question = new Question;
$questionCategory = new QuestionCategory;

/*
 * Insert new record for question
 */
$question->title = $title;
$question->user_id = Auth::user()->user_id;
$question->description = $description;
$question->time_post = date('Y-m-d H:i:s');

if(Input::has('expiredtime'))
    $question->expired_time = Input::get('expiredtime');

$questionCategory->category_id = $category;
$questionCategory->time_added = date('Y-m-d H:i:s');

DB::transaction(function() use ($question, $questionCategory) {

    $question->save();

    /*
     * insert new record for question category
     */
    $questionCategory->question_id = $question->id;
    $questionCategory->save();
});