2014-04-30 73 views
0

我有我不能修復一個問題:要求的作品,與Laravel查詢生成器,似乎不工作

這一要求在phpMyAdmin工作

SELECT cronState.name, command.name, parameter.keyword, eventParameterValue.value 
FROM cronState, commandInstance, command, eventParameterValue, parameter 
WHERE cronState.id = commandInstance.cronState_id 
AND commandInstance.command_id = command.id 
AND eventParameterValue.commandInstance_id = commandInstance.id 
AND eventParameterValue.parameter_id = parameter.id 
AND cronState.id =32 

返回:

name name   keyword  value 

on20 DigitalPinSet State  1 
on20 PwmPinSet  DutyCycle 20 

它是我想要的。

小編輯:我使用AJAX請求(與state_name)這個方法應該返回查詢結果。

現在,當我嘗試使用Laravel Query Builder實現它時,它會返回一些錯誤。

我的代碼:

$cronState_id = DB::table('cronState') 
     ->where('name', Input::get('state_name')) 
     ->first(); 
    $cronEvent = DB::table('cronState') 
     ->join('commandInstance', 'commandInstance.cronState_id', '=', 'cronState.id') 
     ->join('command', 'commandInstance.command_id', '=', 'command.id') 
     ->join('eventParameterValue', 'eventParameterValue.commandInstance_id', '=', 'commandInstance.id') 
     ->join('eventParameterValue', 'eventParameterValue.parameter_id', '=', 'parameter.id') 
     ->where('cronState.id', '=', $cronState_id->id) 
     ->select('cronState.name', 'command.name', 'parameter.keyword', 'eventParameterValue.value'); 

foreach($cronEvent as $result){ 
     $ev = $result['keyword']; 
    } 

return $ev; 

此代碼的錯誤:

{ 「錯誤」:{ 「類型」: 「ErrorException」, 「消息」:「未定義指數: 關鍵字」, 「文件」: 「/無功/網絡/級/應用/控制器/ EventController.php」, 「行」:48}}

如果我改變

foreach($cronEvent as $result){ 
     $ev = $result['keyword']; 
    } 

return $ev; 

return $cronEvent; 

(只要刪除foreach循環,並重新命名返回變量),我有這樣的錯誤:

{ 「錯誤」:{ 「類型」: 「ErrorException」 ,「message」:「類的對象 Illuminate \ Database \ Query \ Builder無法轉換爲 string」,「file」:「/ var/www/stage/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Response.php「,」line「:361}}

那麼,如何才能訪問我的數據中的對象呢?也許我的查詢生成器實施不起作用...

感謝您的任何幫助!

+1

'$ cronEvent'是查詢生成器對象,而不是結果集。你需要調用'get()','first()'或'paginate()'來獲得結果。 – itachi

+0

是的,當然。這是我的一個錯誤...我真是愚蠢的哈哈。它現在很好用。 – Runerod

回答

3

在該查詢生成器的結尾處缺少get(),因此您根本不運行該查詢。而已。

+0

我有時候很愚蠢......因爲那個錯誤而失去了1小時。非常感謝。它現在很好用! – Runerod