Laravel toArray()仍然在DB :: raw中返回一个对象

时间:2017-01-03 15:50:55

标签: php arrays laravel

我有这段代码:

 $response['data'] = DB::table('customers')
                ->select(DB::raw('name,email,address1,address2,postalcode,state_region,country_code,phone,created_at'))
                ->where('user_id',"=",Auth::user()->id)
                ->where('name','like',"%$search_value%")
                ->orWhere('address1',"%$search_value%")
                ->orWhere('email','like',"%$search_value%")
                ->orWhere('address2','like',"%$search_value%")
                ->orWhere('city','like',"%$search_value%")
                ->orWhere('postalcode','like',"%$search_value%")
                ->orWhere('state_region','like',"%$search_value%")
                ->orWhere('country_code','like',"%$search_value%")
                ->orWhere('phone','like',"%$search_value%")
                ->orderBy('name', $order)
                ->limit($length)
                ->offset($start)
                ->get()->toArray();

这个的结果是:

'data' => 
    array (size=10)
      0 => 
        object(stdClass)[194]
          public 'name' => string '|Barbara User' (length=13)
          public 'email' => string 'email@email.com' (length=16)
          public 'address1' => string 'address aaddress' (length=17)
          public 'address2' => null
          public 'postalcode' => string '00000000' (length=10)
          public 'state_region' => string 'GA' (length=2)
          public 'country_code' => string 'US' (length=2)
          public 'phone' => string '12312312312' (length=10)
          public 'created_at' => string '2017-01-02 15:20:20' (length=19)
      1 => 
        object(stdClass)[201]
            public 'name' => string '|Barbara User' (length=13)
          public 'email' => string 'email@email.com' (length=16)
          public 'address1' => string 'address aaddress' (length=17)
          public 'address2' => null
          public 'postalcode' => string '00000000' (length=10)
          public 'state_region' => string 'GA' (length=2)
          public 'country_code' => string 'US' (length=2)
          public 'phone' => string '12312312312' (length=10)
          public 'created_at' => string '2017-01-02 15:20:20' (length=19)
    ....

正如您所看到的,即使我已经执行toArray(),结果中仍有一个对象。

这里似乎有什么问题?

非常感谢您的帮助!谢谢!

4 个答案:

答案 0 :(得分:9)

当您在toArray()上调用Collection时,如果基础项实施Illuminate\Contracts\Support\Arrayable界面,则该集合将尝试在每个项目上调用toArray()。但是,当您使用查询构建器时,结果将是CollectionstdClass个对象,而stdClass个对象是未实现该接口且没有{toArray()的普通对象1}}方法。因此,当您在toArray() CollectionstdClass个对象上调用stdClass时,您将获得Customer个对象的简单数组。

如果您改为使用Eloquent,请定义Collection模型,并使用该模型执行查询,您的结果将是CustomerArrayable模型,这些模型会实现toArray()界面。因此,当您在此Collection上致电toArray()时,它会在Collection中的每个项目上调用map,您的结果将是一个数组数组。

如果由于某种原因,您不想使用Eloquent,则需要手动将项目从对象转换为数组。您可以使用transform上的Collectionmap方法轻松完成此操作。如果您想要返回新的Collection并保留原始transform,请使用Collection;如果您只想修改原始$response['data'] = DB::table('customers') // query conditions, etc ->get() ->map(function ($item, $key) { return (array) $item; }) ->all(); ,请使用from django import db

db.connections.close_all()

答案 1 :(得分:3)

您正在将集合转换为数组,而不是特定的每个模型。

你可以使用Laravel的集合->transform()方法做这样的事情:

    $response['data'] = DB::table('customers')
            ->select(DB::raw('name,email,address1,address2,postalcode,state_region,country_code,phone,created_at'))
            ->where('user_id',"=",Auth::user()->id)
            ->where('name','like',"%$search_value%")
            ->orWhere('address1',"%$search_value%")
            ->orWhere('email','like',"%$search_value%")
            ->orWhere('address2','like',"%$search_value%")
            ->orWhere('city','like',"%$search_value%")
            ->orWhere('postalcode','like',"%$search_value%")
            ->orWhere('state_region','like',"%$search_value%")
            ->orWhere('country_code','like',"%$search_value%")
            ->orWhere('phone','like',"%$search_value%")
            ->orderBy('name', $order)
            ->limit($length)
            ->offset($start)
            ->get();

    $response['data']->transform(function ($item) {
        // return $item->toArray(); // You could do this if you called the query with model
        return (array)$item; // This should work in your case 
    });

这样$ response [' data']将成为数组的集合(而不是数组)。你还可以这样做:

$response['data']->toArray();

将集合转换为数组。

答案 2 :(得分:1)

您可以将集合中的每个对象类型转换为数组:

$response['data']->transform(function($x) {
   return (array) $x; 
})->toArray();

toArray让你从集合回到数组。

答案 3 :(得分:0)

toArray正在将对象集合更改为对象数组,就像应该这样。如果希望集合内的对象也为数组,则需要首先转换每个对象:

$users = DB::table('users')->where([...])->take(1)->get();

$array = $users->map(function($obj){
    return (array) $obj;
})->toArray();