我如何使用Eloquent Laravel 5.5进行交易

时间:2018-04-13 10:23:14

标签: php laravel-5.5

我尝试使用以下代码在Laravel 5.5中使用Transaction with Eloquent:

DB:: transaction(function () {
$project = Project::find($id);
$project->users()->detach();
$project->delete();
});

我收到了错误:

  

方法[交易]不存在。

2 个答案:

答案 0 :(得分:10)

你可以这样试试,

g

答案 1 :(得分:1)

这是一段很长的代码,但却是一个很好的示例,足以说明我如何在此数据库事务中在这里做很多事情。

DB::transaction(function () use ($request) {
        $digitalLibrary =   new DigitalLibrary();

        $digitalLibrary->exhibitor_id               =   $request["exhibitor-id"];
        $digitalLibrary->event_id                   =   $request["event-id"];

        $digitalLibrary->save();
        
        $digitalLibraryIdForFiles                   =   $digitalLibrary->id;
        $digitalLibraryExhibitorIdForFiles          =   $digitalLibrary->exhibitor_id;
        $exhibitorIdForRedirect                     =   $digitalLibraryExhibitorIdForFiles;

        //Repeater
        $arrDocks                                   = $request["group-docs"];
        
        for($count=0; $count < count($arrDocks); $count++){                
            
            //Document Upload
            $parentFolderName                   =   "DIGITAL_LIBRARY";
            $fileNameToStore                    =   "no-logo.tmp";

            $clientNameFolder                   =   "";
            $exhibitorName                      =   Exhibitor::find($digitalLibrary->exhibitor_id)->exhibitor_name;
            
            if (Str::contains($exhibitorName," ")){
                $clientName = explode(" ", $exhibitorName);    
                if(count($exhibitorName) > 1){
                    $fileName = $exhibitorName[0] . '_' . $exhibitorName[1];
                }
            }
            else{
                $fileName = $exhibitorName;
            }

            if($arrDocks[$count]["upload-document"])
            {
                $logoFileNameWithExtension = $arrDocks[$count]["upload-document"]->getClientOriginalName();            
                $exhibitorNameFolder = $fileName;
                $fileExtension=$arrDocks[$count]["upload-document"]->getClientOriginalExtension();
                $docFileNameToStore = $fileName.'_'.time().'_.'.$fileExtension;
                $path = $arrDocks[$count]["upload-document"]->storeAs('public/DOCUMENTS/'.$parentFolderName.'/'.$exhibitorNameFolder.'/files', $docFileNameToStore);
            }

            //Document Upload End
            $files                              =   new DigitalLibraryFiles();
            $files->digital_library_id          =   $digitalLibraryIdForFiles;
            $files->exhibitor_id                =   $digitalLibraryExhibitorIdForFiles;
            
            $files->file_type_id                =   $arrDocks[$count]["document-type-select"];
            $files->file_name                   =   $docFileNameToStore;
            
            $files->save();
        }
        //Repeater End


        //Image Multi-upload
        $fileNameToStore="no-image.jpg";

        if(request('gallery-image'))
        {
            $i=0;
            foreach($request->file('gallery-image') as $thisImage){
                $fileNameWithExtension          = $thisImage->getClientOriginalName();
                $memberName                     = Exhibitor::find($digitalLibrary->exhibitor_id)->exhibitor_name;;
                $memberNameFolder               = "";
                
                if (Str::contains($memberName," ")){
                    $memberName = explode(" ", $memberName);    
                    if(count($memberName) > 1){
                        $fileName = $memberName[0] . '_' . $memberName[1];
                    }
                }
                else{
                    $fileName = $memberName;
                }
                $memberNameFolder = $fileName;

                $extension=$thisImage->getClientOriginalExtension();
                
                $fileNameToStore = $fileName.'_'.time().$i.'.'.$extension;

                $path= $thisImage->storeAs('public/images/DIGITAL_LIBRARY/'.$memberNameFolder.'/digital-lib-images',$fileNameToStore);


                $libImages                              =   new DigitalLibraryImage();
                $libImages->digital_library_id          =   $digitalLibraryIdForFiles;
                $libImages->exhibitor_id                =   $digitalLibraryExhibitorIdForFiles;
                $libImages->event_id                    =   '';
                $libImages->client_id                   =   '';
                $libImages->is_active                   =   1;
                $libImages->file_name                   =   $fileNameToStore;

                $libImages->save();
                $i++;
            }
        }
    });

**即使进行交易回滚,文件上传仍然会发生。