Search code examples
phpforeachlaraveleloquentlaravel-3

Laravel 3 for each through a returned array using PDO::FETCH_ASSOC


I am using Laravel 3, I think it's awesome. OK, I set my DB config to return assoc arrays to help me out. My controller works fine, the problem I am having is: How do I pass the multiple arrays returned to a blade template for cycling thru a for each?

eg:

 Route::get('allprop', function() {

    $data= User::find(165)->property;

});

This returns:

 array(45) {
  [0]=>
  object(Property)#42 (5) {
    ["attributes"]=>
   array(52) {
  ["id"]=>
  int(71)
  ["code"]=>
  string(32) "b073d07a1357bebf0a6c8b0a193a561b"
  ["ownerid"]=>
  int(165)
  ["profileid"]=>
  int(23)
  ["unit_num"]=>
  string(3) "101"
  ["street_num"]=>
  string(3) "498"
  ["address1"]=>
  string(10) "Mifarm Rd."
  ["address2"]=>
  string(8) "Address2"
  ["city"]=>
  string(11) "White Stone"
  ["county"]=>
  string(4) "York"
  ["subdivision"]=>
  string(11) "White Stone"
  ["state"]=>
  string(2) "VA"
  ["postcode"]=>

When I pass the $data array to the template, I get lost on how to access each property of the array

it says variable $data not found when I try to :

 foreach ($data as $thumb){
  echo $thumb->postcode;
  }

Any help would be great, I know it is probably simple, but I can't seem to find an answer in the Docs or on Code Happy...

Thanks


Solution

  • In order to access $data in your template, you need to pass it to your template like this:

    return View::make("yourtemplate")
        ->with("data", $data)