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?

7
  • 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" ...] Commented Jan 18, 2019 at 0: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 Commented Jan 18, 2019 at 0: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 Commented Jan 18, 2019 at 0:19
  • hmmm so I think the easiest way would be get_object_vars($collection->first()); Commented Jan 18, 2019 at 0: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 Commented Jan 18, 2019 at 0:25

2 Answers 2

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', ...]
Sign up to request clarification or add additional context in comments.

Comments

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])

Comments

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.