Laravel - DB的意外输出

时间:2017-05-15 13:59:18

标签: php laravel types

我有奇怪的问题。 我有动态创建表。这就是我使用DB

的原因
$spec = DB::table($specjalizacja)->where('pacjent_id','=', $pid)->orderBy('created_at')->get();

它返回一些数组集合:

dd($spec):
Collection {#257 ▼
  #items: array:2 [▼
    0 => {#262 ▼
      +"id": "3"
      +"pacjent_id": "1"
      +"wizyta_id": "5"
      +"a": "111"
      +"b": "abcdefg"
      +"c": "3"
      +"created_at": "2017-05-15 14:41:53"
      +"updated_at": "0000-00-00 00:00:00"
    }
    1 => {#254 ▼
      +"id": "4"
      +"pacjent_id": "1"
      +"wizyta_id": "6"
      +"a": "2222222"
      +"b": "ddddddddd"
      +"c": "3"
      +"created_at": "2017-05-15 14:41:58"
      +"updated_at": "0000-00-00 00:00:00"
    }
  ]
}

问题是它与Model实例返回的数据类型不相似。 F.e

$wizyty = Kolejka::where('data','LIKE',$dzis.'%')->where('odbyta','=','0')->where('lekarz_id','=',session('id'))->orderBy('data')->get();

dd($poprzednia):
Collection {#264 ▼
#items: array:2 [▼
0 => Kolejka {#274 ▼
  +fillable: array:30 [▶]
  #connection: "mysql"
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:10 [▼
    "id" => "5"
    "lekarz_id" => "1"
    "pacjent_id" => "1"
    "data" => "2017-05-15 11:00:00"
    "odbyta" => "1"
    "leki" => null
    "uwagi" => null
    "specjalizacja" => "1"
    "created_at" => "2017-05-15 09:52:18"
    "updated_at" => "2017-05-15 09:52:33"
  ]
  #original: array:10 [▶]
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #events: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}
1 => Kolejka {#275 ▶}
]
}

现在我认为我可以这样做:

@foreach($poprzednia as $pw)
  {{$pw}}
@endforeach

但如果我这样做

@foreach($spec as $sp)
  {{$sp}}
@endforeach

或者类似于通过执行$ sp [0]或任何基本上我没有成功来获取价值的任何事情并最终导致错误htmlspecialchars() expects parameter 1 to be string, object given

{{$ spec}}在视图中:(不起作用)

[{"id":"3","pacjent_id":"1","wizyta_id":"5","a":"111","b":"abcdefg","c":"3","created_at":"2017-05-15 14:41:53","updated_at":"0000-00-00 00:00:00"},{"id":"4","pacjent_id":"1","wizyta_id":"6","a":"2222222","b":"ddddddddd","c":"3","created_at":"2017-05-15 14:41:58","updated_at":"0000-00-00 00:00:00"}] 
视图中的

{{$ poprzednia}}(有效)

[{"id":5,"lekarz_id":"1","pacjent_id":"1","data":"2017-05-15 11:00:00","odbyta":"1","leki":null,"uwagi":null,"specjalizacja":"1","created_at":"2017-05-15 09:52:18","updated_at":"2017-05-15 09:52:33"},{"id":6,"lekarz_id":"1","pacjent_id":"1","data":"2017-05-15 11:15:00","odbyta":"1","leki":"x","uwagi":"d","specjalizacja":"1","created_at":"2017-05-15 09:52:53","updated_at":"2017-05-15 09:53:31"}]

2 个答案:

答案 0 :(得分:2)

对于两个输出,您都以相同的方式访问循环内的集合对象。

@foreach($spec as $sp)
  {{ $sp->id }}
@endforeach

答案 1 :(得分:0)

你是否正在尝试使用php json_decode()的功能你只需要这样做:

{{ json_decode($spec) }} 

或在foreach

@foreach($spec as $sp)
  {{ json_decode($sp) }}
@endforeach