1
$results = \DB::table('listings')
            ->select(['listings.id','listings.marina_id',\DB::raw(['min(special_offers.price) as sp_min_price','max(special_offers.price) as sp_max_price'])])
            ->join('special_offers','listings.id','=','special_offers.listing_id')
            ->where('special_offers.status',1)
            ->where("listings.status", \App\Listing::PUBLISHED)
            ->where('listings.type', \App\Listing::CHARTER)
            ->orderBy('special_offers.price','asc');
        $res = $results->get();
        \Log::info($res->sp_min_price." ".$res->sp_max_price);

ERROR: Array to string conversion

* Convert an array of column names into a delimited string.
 *
 * @param  array   $columns
 * @return string
 */
public function columnize(array $columns)
{
    return implode(', ', array_map(array($this, 'wrap'), $columns));
}

Why I get this error message? How can I solve it?

Thank you!

UPDATE: I changed the code like this:

$min = \DB::table('listings')
            ->selectRaw(['min(special_offers.price) as sp_min_price','max(special_offers.price) as sp_max_price'])
            ->join('special_offers','listings.id','=','special_offers.listing_id')
            ->where('special_offers.status',1)
            ->where('listings.status',\App\Listing::PUBLISHED)
            ->where('listings-type',\App\Listing::CHARTER)
            ->get();
        foreach ( $min as $sp_min ) {
            \Log::info ( $sp_min->sp_min_price." ".$sp_min->sp_max_price);
        }

but still the same error..

FranCode
  • 93
  • 1
  • 7
  • `$res` this would give you array of object so `$res->sp_min_price` will produce error instead you can iterate over result or try like `$res[0]->sp_min_price` same goes for `sp_max_price` – M Khalid Junaid Dec 21 '17 at 10:20
  • both `selectRaw` and `\DB:raw` expect a **string** as their first parameter, not an array. I suspect that is the problem. If not, can you post the full stacktrace of the exception? – Pevara Dec 21 '17 at 10:53

1 Answers1

0

get() will return a collection of objects but you're trying to use it as string. So, change this:

\Log::info($res->sp_min_price." ".$res->sp_max_price);

To:

\Log::info($res);

To log results.

Or you can log only sp_min_price and sp_max_price for all fetched rows:

\Log::info($res->pluck('sp_min_price')->toArray());
\Log::info($res->pluck('sp_max_price')->toArray());
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279