如何匹配两个表并将其插入laravel 5.4中的另一个表?

时间:2017-11-07 21:36:14

标签: php mysql laravel eloquent laravel-5.4

我需要匹配两个表并将其插入到支出表中,所以这是我的代码......

    public function store(Request $request)
{
    //Approved Request
    $approvedRequest= DB::table('request')
    ->where('users_MemId',Auth::user()->MemId)
    ->where('requestStatus','Approved')
    ->join('requestdetails','request.requestId','=','requestdetails.request_requestId')
    ->join('itemattribute','requestdetails.RequestDetailsId','=','itemattribute.RequestDetailsId')
    ->join('exudeinventory', 'itemattribute.AttrName', '=', 'exudeinventory.ItemName')
    ->select('itemattribute.AttrName', 'exudeinventory.InventoryId', 'request.requestId')
    ->get();
    // dd($approvedRequest);

存储功能。

store function

我已经尝试过匹配这些表格了,我无法弄清楚如何在另一个表格中插入这些值,或者我是否正确行事。

disbursment table

exudeinventory table

itemattribute table

我一直在寻找解决方案,但我仍然无法理解,希望有人能帮助我并向我解释,我是laravel T的新手,T

1 个答案:

答案 0 :(得分:0)

也许你可以这样做:

        public function store(Request $request)
        {
            //Approved Request
            $approvedRequest= DB::table('request')
            ->where('users_MemId',Auth::user()->MemId)
            ->where('requestStatus','Approved')
            ->join('requestdetails','request.requestId','=','requestdetails.request_requestId')
            ->join('itemattribute','requestdetails.RequestDetailsId','=','itemattribute.RequestDetailsId')
            ->join('exudeinventory', 'itemattribute.AttrName', '=', 'exudeinventory.ItemName')
            ->select('itemattribute.AttrName', 'exudeinventory.InventoryId', 'request.requestId')
            ->get()
            ->each (function ($request, $key) {
                $dis = new Disbursment;
                $dis->DisbursmentId = rand(1, 999999);
                $dis->ItemImage = 'default.jpg';
                $dis->ItemTypeId = $request->AttrName;
                $dis->exudeinventory_InventoryId = $request->InventoryId;
                $dis->request_requestId = $request->requestId;
                $dis->save();
            });
        }