duankan6894 2015-02-23 03:55
浏览 11
已采纳

查询不在Laravel中的函数中工作,但在其他地方工作

I have a function that looks like this:

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

This function however is not returning anything. The reports table however does has an element with a pkey of 1:

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')

And if within this same function I instead do a ::all I get this response:

[{"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"}]

That is with this code:

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

So I am a little confused here why this is not working correctly. Also; another function within the controller is using a query and it has no troubles returning the correct output. This is the code of the other function:

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

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

I am wondering what I am doing incorrectly here? As a side note, this is being called via a post request with a route that looks like this:

Route::post('reports/update', 'ReportsController@update');

Doubt that matters, but extra information doesn't hurt.

Doing a dd($reports) after the ::find(1) returns the following output:

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

Edit for comment:

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

Getting this:

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
  • 写回答

2条回答 默认 最新

  • draj840143 2015-02-23 05:54
    关注

    Eloquent uses by default id as primary key and find method is looking record by primary key. However you can change your primary key name if you have different schema. In Eloquent model you can specify:

    protected $primaryKey = 'pkey';
    

    and now find will use pkey as primary key.

    You can also set manually your table name in case if it were not reports but for example reporting_system:

    protected $table = 'reporting_system';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型