【问题标题】:Issue in query builder - laravel 5.2查询生成器中的问题 - laravel 5.2
【发布时间】:2016-04-28 23:06:21
【问题描述】:

我有一个查询出错

$query=   DB::table('crm_listings as l')
        ->leftJoin('crm_location as loc', 'l.area_location_id', '=', 'loc.loc_id')
        ->select('l.id','l.name','l.price', 'loc.lat', 'loc.lon')
  ->get();

  foreach($query as $i=>$p) { 

    $images_query =DB::table('crm_listings_images')->where('listing_id', $p['id'])->select('image')->get();
     $query[$i]['images'] = $images_query;

  }
  return $query;

错误是Cannot use object of type stdClass as array

我有这些问题

  1. 这是一个不错的方法吗?或者我们可以改进它?
  2. 如何解决此错误?

【问题讨论】:

  • 哪一行你有这个错误?正如它清楚地说你正在尝试将对象用作数组。其次,您可以使用Relationships 使其干净。

标签: laravel-5.2 laravel-query-builder


【解决方案1】:

你必须将 eloquent 的结果转换为数组

->get()->toarray();

【讨论】:

  • DB::table('crm_listings as l') ->leftJoin('crm_location as loc', 'l.area_location_id', '=', 'loc.loc_id') ->select(' l.id','l.name','l.price','loc.lat','loc.lon') ->get()->toarray()
【解决方案2】:
  • 看看 Eloquent Relationships

  • 使用$p->id 而不是$p['id']

【讨论】:

    猜你喜欢
    • 2018-11-16
    • 1970-01-01
    • 2018-12-30
    • 2021-08-11
    • 2019-03-29
    • 2021-01-10
    • 2017-08-13
    • 2015-08-06
    • 1970-01-01
    相关资源
    最近更新 更多