数据库查询laravel

时间:2017-12-21 10:18:06

标签: php sql laravel

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

但仍然是同样的错误..

1 个答案:

答案 0 :(得分:0)

get() will return a collection of objects但您尝试将其用作字符串。所以,改变这个:

#define N sizeof(T)
char buf[N];
T obj;                          // obj initialized to its original value
std::memcpy(buf, &obj, N);      // between these two calls to std​::​memcpy, obj might be modified
std::memcpy(&obj, buf, N);      // at this point, each subobject of obj of scalar type holds its original value

要:

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

记录结果。

或者,您只能为所有提取的行记录\Log::info($res); sp_min_price

sp_max_price