简体   繁体   English

与laravel元素进行交易

[英]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 我现在有laravel项目,现在我有一个项目表,该表与像item_state item_colors item_barcode等多个ather表连接起来,现在在添加项目的页面中,它的存储功能还可以,我有此代码

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 . 现在我想做交易,如果没有添加项目或代码中的任何错误,则在ather表中什么也不会发生,就像laravel中的交易一样,但是它不适用于高级模型。

You would be talking about Database Transactions in this case: 在这种情况下,您将谈论数据库事务:

https://laravel.com/docs/5.5/database#database-transactions 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 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();
}
DB::beginTransaction();
try{
   //Code which can  throw error
   DB::commit();
}catch(Exception $e){
   DB::rollBack();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM