0

I have weird problem. I have dynamically created tables. That's why I'm using DB

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

It returns something a collection of arrays:

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

The problem is that it's not similar to datatype that Model instance is returning. 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 ▶}
]
}

Now in my view I can do something like that:

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

But if I do

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

Or anything like trying to get value by doing $sp[0] or anything basically I am unsuccessful and end up with an error of htmlspecialchars() expects parameter 1 to be string, object given

{{$spec}} in view: (doesnt work)

[{"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}} in view (that works)

[{"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
  • Try to dd($sp[0]->a) Commented May 15, 2017 at 14:08
  • The different here: DB::table($specjalizacja) And Kolejka::where. One is DB Query and one is Laravel Query Builder Commented May 15, 2017 at 14:09

2 Answers 2

2

You access the collection object inside the loop the same way for both the output.

@foreach($spec as $sp)
  {{ $sp->id }}
@endforeach
Sign up to request clarification or add additional context in comments.

2 Comments

That seems to be working just fine. The problem I have now is how can i possibly access all this data if i do not know how columns are named? Can I somehow print all of it? Or access it by index?
Try {{ $sp->all() }}
0

Are you trying use the function of php json_decode() You just need to do this:

{{ json_decode($spec) }} 

or in the foreach

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

1 Comment

Thats is strange. Can you try to convert the collection in to array using this function $spec->toArray()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.