0

I have a object (Illuminate\Database\Eloquent\Builder) that have a (Illuminate\Database\Query\Builder) inside it. I dump the object and can see this:

The image bellow, is a dd($querybuilder) from the object

My plan is to get the query and extract the selected columns but I already tried ($querybuilder->query) and ($querybuilder['query']) and I received errors.

How can I get the "columns" value?


Solved. Thank you Tim Lewis

$query->getQuery()->columns worked!

Im posting where what im tying to do. select and addSelect are not doing what I wanted if no colunms are selected.

    public function scopeAddStatusColumn($query)
{
    $previous_columns = $query->getQuery()->columns;
    $status_column = DB::raw('(begin <= NOW() AND end >= NOW()) OR (begin <= NOW() AND end IS NULL) as status');
    $new_columns = $previous_columns == null ? ['*', $status_column] : [$previous_columns] + [$status_column];
    $query = $query->select($new_columns);
    return $query;
}
  • Please include your code for this `$queryBuilder` variable; a `dd($queryBuilder)` is helpful, but so is including how you're defining the variable. Also, you can check [this page](https://laravel.com/api/9.x/Illuminate/Database/Query/Builder.html) for all `Builder` class properties and methods. I don't see an accessor for the `$columns` attribute, but it is `public`, so `$queryBuilder->columns` should return them (`null` in your case). Edit: The `columns` property is part of `Query`, so `$queryBuilder->getQuery()->columns` – Tim Lewis Mar 31 '22 at 18:39
  • If you want to get certain columns, then [select](https://laravel.com/docs/master/queries#select-statements) is what you need. If you want to know what columns are in the table, then you'll need to [query the schema](https://stackoverflow.com/questions/37157270/how-to-select-all-column-name-from-a-table-in-laravel). – aynber Mar 31 '22 at 18:41

1 Answers1

0

If you want to get all the columns of the table:

$query = Products::where('product_name', 'pencil')->get();

If you want to get a spesific column:

$query = Products::where('product_name', 'pencil')->get(['product_id']); // this is an array, you can get any column name here

If you want to get value of a single column:

$query = Products::where('product_name', 'pencil')->first(['description'])->description;