“Response内容必须是实现__toString()的字符串或对象,”boolean“给出。”

时间:2015-11-08 09:30:47

标签: php json laravel laravel-5 eloquent

我正在尝试使用AJAX json检索记录,并且我正在尝试通过JSON格式将结果数据发送回客户端。 (以下是我的尝试)

$vote = users_details::where('vote', '=', 1)->get();
return response()->json(['success' => true, 'vote' => $vote ]);

但不幸的是,遗憾的是没有工作,它让我犯了这个错误。

  

“Response内容必须是实现的字符串或对象   __toString(),“boolean”给出。“

我试过

dd(var_dump($vote));

它向我显示检索到的数据,所以我猜问题是在返回JSON部分,我甚至尝试过

$vote = users_details::where('vote', '=', 1)->get()->toJson();
return response()->json(['success' => true, 'vote' => $vote ]);

并且它抛出了这个错误(参见下文)

  

调用未定义的函数App \ Http \ Controllers \ toJson()

任何想法,帮助,建议,推荐,线索好吗?

更新:使用

检索的数据
dd(var_dump(users_details::where('vote', '=', 1)->get(['firstname', 'middlename', 'lastname', 'branch', 'position', 'img'])));

结果

object(Illuminate\Database\Eloquent\Collection)[205]
protected 'items' => 
array (size=12)
  0 => 
    object(App\users_details)[206]
      protected 'table' => string 'employee' (length=8)
      protected 'connection' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=6)
          ...
      protected 'original' => 
        array (size=6)
          ...
      protected 'relations' => 
        array (size=0)
          ...
      protected 'hidden' => 
        array (size=0)
          ...
      protected 'visible' => 
        array (size=0)
          ...
      protected 'appends' => 
        array (size=0)
          ...
      protected 'fillable' => 
        array (size=0)
          ...
      protected 'guarded' => 
        array (size=1)
          ...
      protected 'dates' => 
        array (size=0)
          ...
      protected 'casts' => 
        array (size=0)
          ...
      protected 'touches' => 
        array (size=0)
          ...
      protected 'observables' => 
        array (size=0)
          ...
      protected 'with' => 
        array (size=0)
          ...
      protected 'morphClass' => null
      public 'exists' => boolean true
  1 => 
    object(App\users_details)[207]
      protected 'table' => string 'employee' (length=8)
      protected 'connection' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=6)
          ...
      protected 'original' => 
        array (size=6)
          ...
      protected 'relations' => 
        array (size=0)
          ...
      protected 'hidden' => 
        array (size=0)
          ...
      protected 'visible' => 
        array (size=0)
          ...
      protected 'appends' => 
        array (size=0)
          ...
      protected 'fillable' => 
        array (size=0)
          ...
      protected 'guarded' => 
        array (size=1)
          ...
      protected 'dates' => 
        array (size=0)
          ...
      protected 'casts' => 
        array (size=0)
          ...
      protected 'touches' => 
        array (size=0)
          ...
      protected 'observables' => 
        array (size=0)
          ...
      protected 'with' => 
        array (size=0)
          ...
      protected 'morphClass' => null
      public 'exists' => boolean true
  2 => 
    object(App\users_details)[208]
      protected 'table' => string 'employee' (length=8)
      protected 'connection' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=6)
          ...
      protected 'original' => 
        array (size=6)
          ...
      protected 'relations' => 
        array (size=0)
          ...
      protected 'hidden' => 
        array (size=0)
          ...
      protected 'visible' => 
        array (size=0)
          ...
      protected 'appends' => 
        array (size=0)
          ...
      protected 'fillable' => 
        array (size=0)
          ...
      protected 'guarded' => 
        array (size=1)
          ...
      protected 'dates' => 
        array (size=0)
          ...
      protected 'casts' => 
        array (size=0)
          ...
      protected 'touches' => 
        array (size=0)
          ...
      protected 'observables' => 
        array (size=0)
          ...
      protected 'with' => 
        array (size=0)
          ...
      protected 'morphClass' => null
      public 'exists' => boolean true
  3 => 
    object(App\users_details)[209]
      protected 'table' => string 'employee' (length=8)
      protected 'connection' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=6)
          ...
      protected 'original' => 
        array (size=6)
          ...
      protected 'relations' => 
        array (size=0)
          ...
      protected 'hidden' => 
        array (size=0)
          ...
      protected 'visible' => 
        array (size=0)
          ...
      protected 'appends' => 
        array (size=0)
          ...
      protected 'fillable' => 
        array (size=0)
          ...
      protected 'guarded' => 
        array (size=1)
          ...
      protected 'dates' => 
        array (size=0)
          ...
      protected 'casts' => 
        array (size=0)
          ...
      protected 'touches' => 
        array (size=0)
          ...
      protected 'observables' => 
        array (size=0)
          ...
      protected 'with' => 
        array (size=0)
          ...
      protected 'morphClass' => null
      public 'exists' => boolean true
  4 => 
    object(App\users_details)[210]
      protected 'table' => string 'employee' (length=8)
      protected 'connection' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=6)
          ...
      protected 'original' => 
        array (size=6)
          ...
      protected 'relations' => 
        array (size=0)
          ...
      protected 'hidden' => 
        array (size=0)
          ...
      protected 'visible' => 
        array (size=0)
          ...
      protected 'appends' => 
        array (size=0)
          ...
      protected 'fillable' => 
        array (size=0)
          ...
      protected 'guarded' => 
        array (size=1)
          ...
      protected 'dates' => 
        array (size=0)
          ...
      protected 'casts' => 
        array (size=0)
          ...
      protected 'touches' => 
        array (size=0)
          ...
      protected 'observables' => 
        array (size=0)
          ...
      protected 'with' => 
        array (size=0)
          ...
      protected 'morphClass' => null
      public 'exists' => boolean true
  5 => 
    object(App\users_details)[211]
      protected 'table' => string 'employee' (length=8)
      protected 'connection' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=6)
          ...
      protected 'original' => 
        array (size=6)
          ...
      protected 'relations' => 
        array (size=0)
          ...
      protected 'hidden' => 
        array (size=0)
          ...
      protected 'visible' => 
        array (size=0)
          ...
      protected 'appends' => 
        array (size=0)
          ...
      protected 'fillable' => 
        array (size=0)
          ...
      protected 'guarded' => 
        array (size=1)
          ...
      protected 'dates' => 
        array (size=0)
          ...
      protected 'casts' => 
        array (size=0)
          ...
      protected 'touches' => 
        array (size=0)
          ...
      protected 'observables' => 
        array (size=0)
          ...
      protected 'with' => 
        array (size=0)
          ...
      protected 'morphClass' => null
      public 'exists' => boolean true
  6 => 
    object(App\users_details)[212]
      protected 'table' => string 'employee' (length=8)
      protected 'connection' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=6)
          ...
      protected 'original' => 
        array (size=6)
          ...
      protected 'relations' => 
        array (size=0)
          ...
      protected 'hidden' => 
        array (size=0)
          ...
      protected 'visible' => 
        array (size=0)
          ...
      protected 'appends' => 
        array (size=0)
          ...
      protected 'fillable' => 
        array (size=0)
          ...
      protected 'guarded' => 
        array (size=1)
          ...
      protected 'dates' => 
        array (size=0)
          ...
      protected 'casts' => 
        array (size=0)
          ...
      protected 'touches' => 
        array (size=0)
          ...
      protected 'observables' => 
        array (size=0)
          ...
      protected 'with' => 
        array (size=0)
          ...
      protected 'morphClass' => null
      public 'exists' => boolean true
  7 => 
    object(App\users_details)[213]
      protected 'table' => string 'employee' (length=8)
      protected 'connection' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=6)
          ...
      protected 'original' => 
        array (size=6)
          ...
      protected 'relations' => 
        array (size=0)
          ...
      protected 'hidden' => 
        array (size=0)
          ...
      protected 'visible' => 
        array (size=0)
          ...
      protected 'appends' => 
        array (size=0)
          ...
      protected 'fillable' => 
        array (size=0)
          ...
      protected 'guarded' => 
        array (size=1)
          ...
      protected 'dates' => 
        array (size=0)
          ...
      protected 'casts' => 
        array (size=0)
          ...
      protected 'touches' => 
        array (size=0)
          ...
      protected 'observables' => 
        array (size=0)
          ...
      protected 'with' => 
        array (size=0)
          ...
      protected 'morphClass' => null
      public 'exists' => boolean true
  8 => 
    object(App\users_details)[214]
      protected 'table' => string 'employee' (length=8)
      protected 'connection' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=6)
          ...
      protected 'original' => 
        array (size=6)
          ...
      protected 'relations' => 
        array (size=0)
          ...
      protected 'hidden' => 
        array (size=0)
          ...
      protected 'visible' => 
        array (size=0)
          ...
      protected 'appends' => 
        array (size=0)
          ...
      protected 'fillable' => 
        array (size=0)
          ...
      protected 'guarded' => 
        array (size=1)
          ...
      protected 'dates' => 
        array (size=0)
          ...
      protected 'casts' => 
        array (size=0)
          ...
      protected 'touches' => 
        array (size=0)
          ...
      protected 'observables' => 
        array (size=0)
          ...
      protected 'with' => 
        array (size=0)
          ...
      protected 'morphClass' => null
      public 'exists' => boolean true
  9 => 
    object(App\users_details)[215]
      protected 'table' => string 'employee' (length=8)
      protected 'connection' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=6)
          ...
      protected 'original' => 
        array (size=6)
          ...
      protected 'relations' => 
        array (size=0)
          ...
      protected 'hidden' => 
        array (size=0)
          ...
      protected 'visible' => 
        array (size=0)
          ...
      protected 'appends' => 
        array (size=0)
          ...
      protected 'fillable' => 
        array (size=0)
          ...
      protected 'guarded' => 
        array (size=1)
          ...
      protected 'dates' => 
        array (size=0)
          ...
      protected 'casts' => 
        array (size=0)
          ...
      protected 'touches' => 
        array (size=0)
          ...
      protected 'observables' => 
        array (size=0)
          ...
      protected 'with' => 
        array (size=0)
          ...
      protected 'morphClass' => null
      public 'exists' => boolean true
  10 => 
    object(App\users_details)[216]
      protected 'table' => string 'employee' (length=8)
      protected 'connection' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=6)
          ...
      protected 'original' => 
        array (size=6)
          ...
      protected 'relations' => 
        array (size=0)
          ...
      protected 'hidden' => 
        array (size=0)
          ...
      protected 'visible' => 
        array (size=0)
          ...
      protected 'appends' => 
        array (size=0)
          ...
      protected 'fillable' => 
        array (size=0)
          ...
      protected 'guarded' => 
        array (size=1)
          ...
      protected 'dates' => 
        array (size=0)
          ...
      protected 'casts' => 
        array (size=0)
          ...
      protected 'touches' => 
        array (size=0)
          ...
      protected 'observables' => 
        array (size=0)
          ...
      protected 'with' => 
        array (size=0)
          ...
      protected 'morphClass' => null
      public 'exists' => boolean true
  11 => 
    object(App\users_details)[217]
      protected 'table' => string 'employee' (length=8)
      protected 'connection' => null
      protected 'primaryKey' => string 'id' (length=2)
      protected 'perPage' => int 15
      public 'incrementing' => boolean true
      public 'timestamps' => boolean true
      protected 'attributes' => 
        array (size=6)
          ...
      protected 'original' => 
        array (size=6)
          ...
      protected 'relations' => 
        array (size=0)
          ...
      protected 'hidden' => 
        array (size=0)
          ...
      protected 'visible' => 
        array (size=0)
          ...
      protected 'appends' => 
        array (size=0)
          ...
      protected 'fillable' => 
        array (size=0)
          ...
      protected 'guarded' => 
        array (size=1)
          ...
      protected 'dates' => 
        array (size=0)
          ...
      protected 'casts' => 
        array (size=0)
          ...
      protected 'touches' => 
        array (size=0)
          ...
      protected 'observables' => 
        array (size=0)
          ...
      protected 'with' => 
        array (size=0)
          ...
      protected 'morphClass' => null
      public 'exists' => boolean true

0 个答案:

没有答案