Awar Pulldozer
Awar Pulldozer

Reputation: 1101

transact with laravel elequent

i have laravel project now i have item table which connect with multi ather table like item_state item_colors item_barcode ect now in the page of adding items its ok in store function i have this code

public function store(Request $request)
{
    $item = new Item;
    $item->item_id = $request['item_id'];
    $item->item_desc_ar = $request['item_desc_ar'];
    $item->item_desc_en = $request['item_desc_en'];
    $item->item_default_unit = $request['item_default_unit'];
    $item->item_color = $request['item_color'];
    $item->item_width = $request['item_width'];
    $item->item_length = $request['item_length'];
    $item->item_height = $request['item_height'];
    $item->item_inserter = Auth::user()->id;
    $item->item_state = $request['item_state'];
    $item->item_note = $request['item_note'];
    $item->save();
    $item_barcode = new Item_barcode;
    $item_barcode->item_barcode_item_id = $item->id;
    $item_barcode->item_barcode_barcode = $request->item_barcode;
    $item_barcode->item_barcode_unit = $request->item_default_unit;
    $item_barcode->save();
    foreach($request->category_id as $item_categorys)
    {
        $item_category = new item_category;
        $item_category->category_id = $item_categorys;
        $item_category->item_id = $item->id;
        $item_category->save();
    }
    $item_price = new Item_price;
    $item_price->item_price_item_id = $item->id;
    $item_price->item_price_price = $request->item_price;
    $item_price->item_price_unit_id = $request->item_default_unit;
    $item_price->save();
    $count = 1;
    foreach($request->image as $image)
    {
        if(!empty($image))
        {
            $image_id = $item['id'];
            if(!file_exists(public_path()."/uploads/items/$image_id"))
                File::makeDirectory(base_path()."/public/uploads/items/$image_id");     
            $file = $image;
            $file->move('uploads/items/'.$image_id,$count.'.jpg');
        }
        $count++;
    }
}

now i want to do transact thats if the item not added or any wrong in the code nothing happen in the ather table like the transact in laravel but its not working with elequent model thanks .

Upvotes: 0

Views: 62

Answers (2)

Ganesh Ghalame
Ganesh Ghalame

Reputation: 7033

DB::beginTransaction();
try{
   //Code which can  throw error
   DB::commit();
}catch(Exception $e){
   DB::rollBack();
}

Upvotes: 1

user320487
user320487

Reputation:

You would be talking about Database Transactions in this case:

https://laravel.com/docs/5.5/database#database-transactions

You need to wrap your query in the transaction closure:

DB::transaction(function () {
    DB::table('users')->update(['votes' => 1]);

    DB::table('posts')->delete();
}, 5);

Or manually begin the transaction:

DB::beginTransaction();

If it fails then:

DB::rollBack();

and finally:

DB::commit();

on success.

Here is a good article detailing transactions.

http://fideloper.com/laravel-database-transactions

DB::beginTransaction();

try {
    $item = new Item;
    $item->item_id = $request['item_id'];
    $item->item_desc_ar = $request['item_desc_ar'];
    $item->item_desc_en = $request['item_desc_en'];
    $item->item_default_unit = $request['item_default_unit'];
    $item->item_color = $request['item_color'];
    $item->item_width = $request['item_width'];
    $item->item_length = $request['item_length'];
    $item->item_height = $request['item_height'];
    $item->item_inserter = Auth::user()->id;
    $item->item_state = $request['item_state'];
    $item->item_note = $request['item_note'];
    $item->save();
    $item_barcode = new Item_barcode;
    $item_barcode->item_barcode_item_id = $item->id;
    $item_barcode->item_barcode_barcode = $request->item_barcode;
    $item_barcode->item_barcode_unit = $request->item_default_unit;
    $item_barcode->save();
    foreach($request->category_id as $item_categorys)
    {
        $item_category = new item_category;
        $item_category->category_id = $item_categorys;
        $item_category->item_id = $item->id;
        $item_category->save();
    }
    $item_price = new Item_price;
    $item_price->item_price_item_id = $item->id;
    $item_price->item_price_price = $request->item_price;
    $item_price->item_price_unit_id = $request->item_default_unit;
    $item_price->save();
    $count = 1;
    foreach($request->image as $image)
    {
        if(!empty($image))
        {
            $image_id = $item['id'];
            if(!file_exists(public_path()."/uploads/items/$image_id"))
                File::makeDirectory(base_path()."/public/uploads/items/$image_id");     
            $file = $image;
            $file->move('uploads/items/'.$image_id,$count.'.jpg');
        }
        $count++;
    }
    DB::commit();
} catch (Exception $e) {
    DB::rollBack();
}

Upvotes: 1

Related Questions