在Laravel中使用内部联接插入选择语句

时间:2018-12-31 04:38:10

标签: php mysql laravel eloquent laravel-query-builder

我想在Laravel中使用带有select语句和内部联接的插入查询。我有原始的mysql查询,当我在MySql Workbench中运行查询时,得到了预期的结果。但是我在Laravel日志中得到了这个错误

我尝试使用Laravel查询生成器和getBindings()函数来做到这一点。

  

local.ERROR:传递给Illuminate \ Database \ Query \ Builder :: cleanBindings()的参数1必须为数组类型,给定字符串,在C:\ wamp \ www \ aquafresh_voyager \ vendor \ laravel \ framework \中调用src \ Illuminate \ Database \ Query \ Builder.php在第860行{“ userId”:1,“ email”:“ admin@admin.com”,“ exception”:“ [object](Symfony \ Component \ Debug \ Exception \ FatalThrowableError(code:0):传递给Illuminate \ Database \ Query \ Builder :: cleanBindings()的参数1必须为数组类型,给定字符串,在C:\ wamp \ www \ aquafresh_voyager \ vendor \ laravel \ framework \中调用src \ Illuminate \ Database \ Query \ Builder.php在860行位于C:\ wamp \ www \ aquafresh_voyager \ vendor \ laravel \ framework \ src \ Illuminate \ Database \ Query \ Builder.php:2856)

这是控制器功能。

public function insertNewRoute(Request $request)
{
    $routeplan = new Routeplan;
    $user_email = auth()->user()->email;
    $RouteCode = $request->route_code;
    $routeplan->RouteplanCode = $request->routePlanCode;
    $routeplan->RouteCode = $RouteCode;
    $routeplan->DriverID = $request->driver;
    $routeplan->SalesmanID = $request->salesman;
    $routeplan->Routercode1 = $request->router_01;
    $routeplan->Routercode2 = $request->router_02;
    $routeplan->VehicleNo = $request->vehicle_no;
    $routeplan->Date = $request->date;
    $routeplan->Createuser = $user_email;
    $routeplan->Status = 'TEMP';
    $routeplan->save(); //this query works !

    $select = Invoice::join('customer', function ($join) {
                $join->on('invoice.CustomerCode', '=', 'customer.CustomerCode');
                $join->on('customer.CustomerCode',
                            'rootplan_product.CustomerCode');
            })->join('invoice', 'invoice.CustomerCode', '=', 
                        'customer.CustomerCode')
            ->select(['customer.CustomerCode','customer.RouteCode', 
                        'TEMP', 'rootplan_product.RouteplanCode', 
                        'invoice.Status'])
            ->where('invoice.Status','DOWNLOADED')
            ->whereIn('customer.RouteCode', $RouteCode)
            ->get();

    $bindings = $select->getBindings();
    $insertQuery = 'insert into rootplan_product 
                    (CustomerCode,RouteCode,STATUS)'. $select->toSql();
    \DB::insert($insertQuery, $bindings);
}

这是原始查询

INSERT INTO rootplan_product (CustomerCode,RouteCode,STATUS) SELECT 
customer.CustomerCode,customer.RouteCode,'TEMP' FROM invoice INNER JOIN 
customer ON invoice.CustomerCode = customer.CustomerCode WHERE 
invoice.Status = 'DOWNLOADED'
AND customer.RouteCode = 'MO-A'

0 个答案:

没有答案