0

I am trying to get the columns of a dynamically created table. I looked at this question to find how to get the columns.

\DB::getSchemaBuilder()->getColumnListing(\DB::table('product_list')
    ->join('product_categories', 'product_list.product_category', '=', 'product_categories.id')
    ->join('product_types', 'product_list.product_type', '=', 'product_types.id')
    ->where('product_list.id', $this->id)
    ->orderBy('product_list.id', 'DESC')
    ->get()
);

However, this is giving me an output of []. If I run the command without encapsulating it with \DB::getSchemaBuilder()->getColumnListing() I get this:

Collection {#652 ▼
  #items: array:1 [▼
    0 => {#650 ▼
      +"id": 3
      +"cost": "150.00"
      +"product_category": 1
      +"product_type": 3
      +"product_score": 0
      +"created_at": "2019-01-16 16:34:29"
      +"updated_at": "2019-01-16 16:34:29"
      +"rating": 0
      +"down_payment": "10.00"
      +"title": "Static"
      +"price_start": "50.00"
      +"price_stop": "150.00"
      +"theme": "Custom"
      +"pages": 4
      +"rbac": 0
      +"portal": 0
      +"external_software": 0
      +"company_supplier": "Iezon Solutions"
    }
  ]
}

How can I get an array of all of the columns?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • maybe you can try to get the first item in the collection and pass it to get_object_vars ? is that what you want ? [ "id", "cost", "product_category" ...] – Eden Reich Jan 18 '19 at 00:15
  • Yeah, I need `['id', 'cost', 'product_category', ...]`. `Maatwebsite\Excel\` expects the column headings passed in separate to the rows. (which is really annoying!) @EdenReich – Jaquarh Jan 18 '19 at 00:19
  • I have just found a 'quick-fix' for my issue, but please! If you know the correct approach to doing this, I'd love to see how its done @EdenReich – Jaquarh Jan 18 '19 at 00:19
  • hmmm so I think the easiest way would be get_object_vars($collection->first()); – Eden Reich Jan 18 '19 at 00:20
  • Yeah, the issue is, the package expects to use a Model. Eloquent doesn't, from my failed attempts and experience, allow you to 'inner join' fluently. I am having to use QueryBuilder within my Model (which is just ugh) to even achieve my functionality but its the only package I've found that works well. That has done exactly what I needed - feel free to make that an answer as it is deffo a better solution to my current! Only, I needed to wrap `array_keys()` around `get_object_vars()` @EdenReich – Jaquarh Jan 18 '19 at 00:25
  • right.. array_keys was missing ;) – Eden Reich Jan 18 '19 at 00:27
  • Feel free to answer the question, as it is in fact a better more strategic solution. Thanks again @EdenReich – Jaquarh Jan 18 '19 at 00:31

2 Answers2

3

If you just need the dynamically created table column names a quick solution is:

array_keys(get_object_vars($collection->first())); // will output ['id', 'cost', 'product_category', ...]
Eden Reich
  • 387
  • 2
  • 7
0

This is probably by far the worst solution to use, but I don't have the time to search through the collection documentation to find a better method of doing this or the way its meant to be done using Laravel functionality.

I decided to convert it toArray(), use the first index [0] and cast it to an array. This then allows me to use array_keys() to find the column names.

array_keys((array) \DB::table('product_list')->join('product_categories', 'product_list.product_category', '=', 'product_categories.id')
            ->join('product_types', 'product_list.product_type', '=', 'product_types.id')
            ->where('product_list.id', $this->id)
            ->orderBy('product_list.id', 'DESC')->get()->toArray()[0])
Jaquarh
  • 6,493
  • 7
  • 34
  • 86