Php 将从数据库获取的数据转换为laravel中的数组

Php 将从数据库获取的数据转换为laravel中的数组,php,database,laravel,Php,Database,Laravel,我从“拍卖”表中获取数据 $players = DB::table('auction')->where('id',$id)->get(); 我在这张表格里得到数据 [ { "id": 3, "name": "Yogesh Singh", "year": "BE", "branch": "CE", "division": "D", "achievements": "College Cricket Team", "base_p

我从“拍卖”表中获取数据

$players = DB::table('auction')->where('id',$id)->get();
我在这张表格里得到数据

[
  {
    "id": 3,
    "name": "Yogesh Singh",
    "year": "BE",
    "branch": "CE",
    "division": "D",
    "achievements": "College Cricket Team",
    "base_price": 5000000,
    "status": 1,
    "manager": 0
  }
]
现在我需要将这些数据转移到另一个表“team”中,因此我这样做了

DB::table('team')->insert(['player_id' => $id, 'player_type' => $player->player_type, 'name' => $player->name, 'price' => $player->price]);
它抛出一个错误,表示此集合实例上不存在属性[player_type]

我很确定我无法将从DB获取的数据转换为数组


如何执行此操作?

LAVEL查询生成器select方法返回集合类型的变量。每个成员都属于stdclass类型

集合具有toArraye成员:

请注意,此函数返回二维数组


另一种方法是使用collection->first获取集合的第一个成员,并使用以下方法将这个stdclass转换为数组:

Laravel查询生成器select方法返回集合类型的变量。每个成员都属于stdclass类型

集合具有toArraye成员:

请注意,此函数返回二维数组


另一种方法是使用collection->first获取集合的第一个成员,并使用以下方法将这个stdclass转换为数组:

这是因为
$players
在使用
get()
后返回了
集合。将其更改为
first()
,如果存在,将返回
Model
实例。变量名也是
$player
,而不是
$players

$player = DB::table('auction')->where('id',$id)->first();
现在可以访问以下数据:

DB::table('team')->insert(['player_id' => $id, 'player_type' => $player->player_type, 'name' => $player->name, 'price' => $player->price]);

您可以保留
get()
并访问如下数据:

DB::table('team')->insert(['player_id' => $id, 'player_type' => $player[0]->player_type, 'name' => $player[0]->name, 'price' => $player[0]->price]);

希望对您有所帮助。

这是因为自从您使用
get()
以来,
$players
正在返回一个
集合。将其更改为
first()
,如果存在,将返回
Model
实例。变量名也是
$player
,而不是
$players

$player = DB::table('auction')->where('id',$id)->first();
现在可以访问以下数据:

DB::table('team')->insert(['player_id' => $id, 'player_type' => $player->player_type, 'name' => $player->name, 'price' => $player->price]);

您可以保留
get()
并访问如下数据:

DB::table('team')->insert(['player_id' => $id, 'player_type' => $player[0]->player_type, 'name' => $player[0]->name, 'price' => $player[0]->price]);

希望有帮助。

使用
DB::setFetchMode(PDO::FETCH\u ASSOC)在查询生成器之前这是现在的错误,Class';App\Http\Controllers\PDO';未找到您的拍卖表中是否有“玩家类型”列?您正试图在中获取未知属性ADD
使用PDO
命名空间controller@YogeshKumarSinghuse
DB::setFetchMode(PDO::FETCH_ASSOC)在查询生成器之前这是现在的错误,Class';App\Http\Controllers\PDO';未找到您的拍卖表中是否有“玩家类型”列?您正试图在中获取未知属性ADD
使用PDO
命名空间controller@YogeshKumarSingh