2015-02-23 106 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

相关问题