DB查询laravel出错

问题描述 投票:1回答: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);

错误:数组到字符串转换

* 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));
}

为什么我收到此错误消息?我该如何解决?

谢谢!

更新:我改变了这样的代码:

$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);
        }

但仍然是同样的错误..

php sql laravel
1个回答
0
投票

get() will return a collection of objects,但你正试图用它作为字符串。所以,改变这个:

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

至:

\Log::info($res);

记录结果。

或者,您只能为所有提取的行记录sp_min_pricesp_max_price

\Log::info($res->pluck('sp_min_price')->toArray());
\Log::info($res->pluck('sp_max_price')->toArray());
© www.soinside.com 2019 - 2024. All rights reserved.