2015-11-04 114 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 
] 
); 
相關問題