2015-11-04 82 views
3

在Laravel,为什么我得到一个错误Laravel - 无效参数号:参数没有被定义

无效的参数号:参数没有被定义

我已经包括了所有参数。

当我直接在PHPMyAdmin上测试时,它工作正常。

代码:

$results = \DB::select('SELECT client_id, 
           date_format(start_date,"%d/%m/%Y") as start_date, 
           date_format(end_date,"%d/%m/%Y") as end_date, 
           first_name, last_name, phone, postcode 
          FROM hire INNER JOIN client ON client.id = hire.client_id 
          where ((:sdate between start_date and end_date OR :edate between start_date and end_date) OR (:sdate <= start_date and end_date <= :edate)) AND car_id = :car_id', [ 
     'sdate' => $start_date, 
     'edate' => $end_date, 
     'car_id' => $car_id 
    ] 
); 

变量例如:

$start_date = $inputs['start_date']; //2015-10-27 
$end_date = $inputs['end_date'];  //2015-10-27 
$car_id = $inputs['car_id'];   //5 
+0

Czan你显示了如何为3个变量设置值? –

+0

@MateoBarahona更新了我的问题。 –

回答

2

你将所有的SQL laravel的查询生成器select()方法里面。你只需要使用其他方法:

$select = [ 
    'client_id', 
    'start_date', 
    'end_date', 
    'first_name', 
    'last_name', 
    'phone', 
    'postcode' 
]; 

$results = \DB::table('hire') 
     ->join('client', 'client.id', '=', 'hire.client_id') 
     ->select($select) 
     ->where('car_id', $car_id) 
     ->whereBetween('start_date', [$start_date, $end_date]) 
     ->orWhereBetween('end_date', [$start_date, $end_date]) 
     ->get(); 

这些不是你的所有参数,但它应该让你开始。

如果你不希望使用查询生成器,尝试进行raw($expression)

$results = \DB::raw('SELECT client_id, 
          date_format(start_date,"%d/%m/%Y") as start_date, 
          date_format(end_date,"%d/%m/%Y") as end_date, 
          first_name, last_name, phone, postcode 
         FROM hire INNER JOIN client ON client.id = hire.client_id 
         where ((:sdate between start_date and end_date OR :edate between start_date and end_date) OR (:sdate <= start_date and end_date <= :edate)) AND car_id = :car_id', [ 
     'sdate' => $start_date, 
     'edate' => $end_date, 
     'car_id' => $car_id 
    ] 
); 
+0

我明白,但为什么原始SQL在'select()'中失败? –

+0

您可以尝试使用'selectRaw($ select)'(或'raw($ expression)')来运行您的语句,但使用'select()'只会将列添加到当前查询生成器。它不会处理你正在运行的任何SQL。更不用说你也不会使用get()方法询问任何结果。 https://github.com/laravel/framework/blob/5.1/src/Illuminate/Database/Query/Builder.php#L230 –

+1

虽然使用查询生成器或Eloquent ORM确实是首选,但向原始SQL添加原始SQL 'DB'外观的'select'方法是完全有效的。 http://laravel.com/docs/5.1/database#running-queries –

14

您的查询失败,因为你在你的查询重用参数。 Laravel为其SQL查询使用PDO。根据docs

除非启用了仿真模式,否则不能在准备好的语句中多次使用同名的命名参数标记。

所以,即使它们具有相同的值,您将不得不重新命名这些参数。

$results = \DB::select('SELECT client_id, 
          date_format(start_date,"%d/%m/%Y") as start_date, 
          date_format(end_date,"%d/%m/%Y") as end_date, 
          first_name, last_name, phone, postcode 
         FROM hire INNER JOIN client ON client.id = hire.client_id 
         where ((:sdate between start_date and end_date OR :edate between start_date and end_date) OR (:sdate2 <= start_date and end_date <= :edate2)) AND car_id = :car_id', [ 
    'sdate' => $start_date, 
    'sdate2' => $start_date, 
    'edate' => $end_date, 
    'edate2' => $end_date, 
    'car_id' => $car_id 
] 
); 
相关问题