2015-02-23 83 views
1

我有一個功能,看起來像這樣:查詢不中Laravel功能的工作,但在其他地方工作

public function update() { 
     $reports = Reports::find(1); 
     return $reports; 
    } 

此功能但不返回任何東西。報告表但不具有爲1的p鍵的元素:

INSERT INTO `theDatabase`.`reports` (`pkey`, `id`, `comment`, `youtubeUrl`, `displayName`, `profilePictureUrl`, `approvalStatus`, `rep`, `created_at`, `updated_at`) VALUES (1, '1234567890', 'This is just a test report', 'https://example.com', 'Test User', 'https://example.com/somethingsomething', '0', '1', '2015-02-22 00:00:00', '2015-02-22 00:00:00') 

如果此相同的函數中我,而不是做一個::all我得到這樣的迴應:

[{"pkey":1,"id":"1234567890","comment":"This is just a test report","youtubeUrl":"https:\/\/youtube.com\/watch?32222","displayName":"Test User","profilePictureUrl":"https:\/\/google.com\/somethingsomething","approvalStatus":0,"rep":1,"created_at":"2015-02-22 00:00:00","updated_at":"2015-02-22 00:00:00"},{"pkey":4,"id":"12345678903","comment":"This is just a test report","youtubeUrl":"https:\/\/youtube.com\/watch?32222","displayName":"Test User","profilePictureUrl":"https:\/\/google.com\/somethingsomething","approvalStatus":1,"rep":1,"created_at":"2015-02-22 00:00:00","updated_at":"2015-02-22 00:00:00"}] 

與此代碼:

public function update() { 
     $reports = Reports::all(); 
     return $reports; 
    } 

所以我在這裏有點困惑,爲什麼這不能正常工作。也;控制器中的另一個功能是使用查詢,並且沒有返回正確輸出的麻煩。這是其他功能的代碼:

public function index() { 
     $reports = Reports::all()->where('approvalStatus', '=', 0); 

     return view('reports.index', compact('reports')); 
    } 

我想知道我在這裏做的不正確嗎?作爲一個方面說明,這是正在通過與看起來像這樣的路線POST請求稱爲:

Route::post('reports/update', '[email protected]'); 

疑問的事項,但額外的信息不受到傷害。

做一個dd($reports)::find(1)返回後的輸出如下:

undefined: {undefined: -909362565, " ": null} 
" ": null 
undefined: -909362565 

編輯發表評論:

$reports = Reports::where("pkey",1); 
    dd($reports); 
    return $reports; 

聞聽此事:

ErrorException in Response.php line 406: 
Object of class Illuminate\Database\Eloquent\Builder could not be converted to string 

0: {undefined: {undefined: 2,…}, Protected property: false} 
Protected property: false 
undefined: {undefined: 2,…} 
Protected property: " class=sf-dump-note>Application</abbr> {<a class=sf-dump-ref href=#sf-dump-748334229-ref22 title=" 
undefined: 2 
1: 3 
2: {undefined: 2,…} 
3: "Protected property" 
4: false 
+0

什麼DD($報告)顯示? – Boris 2015-02-23 04:00:51

+0

ID字段是整數字段嗎? – Boris 2015-02-23 04:02:33

+0

@Boris將「dd」輸出添加到OP。 'id'涉及增量列表中的列'pkey'。身份證本身是通過發送請求 – ComputerLocus 2015-02-23 04:05:25

回答

2

默認使用id作爲主鍵,find方法使用主鍵查找記錄。但是,如果您有不同的架構,則可以更改主鍵名稱。在Eloquent模型,你可以指定:

protected $primaryKey = 'pkey'; 

現在find將使用pkey作爲主鍵。

您也可以手動設置你的表名的情況下,如果沒有reports但例如reporting_system

protected $table = 'reporting_system'; 
+0

謝謝你是這個問題,並解決了這個問題。我反而改變了桌子,並沒有意識到這一點,但這絕對是更好的解決方案。 – ComputerLocus 2015-02-23 16:37:55

0

我改變了我的架構,以便pkey現在被命名爲id,和我的舊id列現在被命名爲profileId。我猜find()正在使用id作爲主鍵字段。

+0

我試圖模擬你的情況,並且pkey似乎不是問題,它在這裏完美工作。也許模型中的pkey的聲明? – Boris 2015-02-23 04:44:46

+1

@Boris我不知道爲什麼它不起作用,但我只是知道這些修改已經解決了問題。 – ComputerLocus 2015-02-23 04:45:27

相關問題