Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Larvel-Table::find($id)的奇怪行为,它返回行数组,而不是1行_Php_Laravel_Laravel 5_Laravel 5.2 - Fatal编程技术网

Php Larvel-Table::find($id)的奇怪行为,它返回行数组,而不是1行

Php Larvel-Table::find($id)的奇怪行为,它返回行数组,而不是1行,php,laravel,laravel-5,laravel-5.2,Php,Laravel,Laravel 5,Laravel 5.2,我对Table::find($id)的理解应该等同于Table::where('id',$id)->first(), 但我收到的是数组而不是1条记录 Order.php(模型): 尽职调查结果: Collection {#270 ▼ #items: array:1 [▼ 0 => Order {#271 ▼ +timestamps: true #guarded: array:1 [▶] #hidden: array:2 [▶] #

我对Table::find($id)的理解应该等同于Table::where('id',$id)->first(), 但我收到的是数组而不是1条记录

Order.php(模型):

尽职调查结果:

Collection {#270 ▼
  #items: array:1 [▼
    0 => Order {#271 ▼
      +timestamps: true
      #guarded: array:1 [▶]
      #hidden: array:2 [▶]
      #connection: null
      #table: null
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      #attributes: array:30 [▶]
      #original: array:30 [▶]
      #relations: []
      #visible: []
      #appends: []
      #fillable: []
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
  ]
}
如果我错误地认为find应该返回1行,那么我仍然对此有问题,因为我在删除dd()时收到错误消息:


你改变主键了吗


检查$id是否不是数组。如果它是一个数组,Laravel将返回多行

如果将数组传递给
find
,它将返回一个集合


似乎,
$order\u id
是一个数组。

否,我有id:1 id Primary int(10)无符号No-None自动递增,用户字段是:11 user int(11)No-None。另外,请验证您的$id是否不是数组。如果是这样,Laravel将返回更多行,而不是一行。是的,您是对的,请更改关于传递id为的数组的答案,我将接受它。谢谢你,伙计!原因是我使用了Session::push而不是Session::put to store order_id:(哦!小心并始终检查函数返回的数据类型。:)第二个错误源于第一个错误-您调用的变量
$order
实际上是一个订单集合,该集合没有
save()
方法。重命名变量,使其读取
$list\u of\u orders->user=$user\u id$列出订单->保存()错误应该更明显。对不起,Joseph,CodeContrive在你回答之前23秒编辑了他的帖子。这就是我接受他的答案的原因
Collection {#270 ▼
  #items: array:1 [▼
    0 => Order {#271 ▼
      +timestamps: true
      #guarded: array:1 [▶]
      #hidden: array:2 [▶]
      #connection: null
      #table: null
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      #attributes: array:30 [▶]
      #original: array:30 [▶]
      #relations: []
      #visible: []
      #appends: []
      #fillable: []
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    }
  ]
}
BadMethodCallException in Macroable.php line 81: Method save does not exist.

    in Macroable.php line 81
    at Collection->__call('save', array()) in Order.php line 64
    at Collection->save() in Order.php line 64
    at Order->change_user(array('57'), '18') in RegistersUsers.php line 69
    at AuthController->register(object(Request), object(Order))
    at call_user_func_array(array(object(AuthController), 'register'), array(object(Request), object(Order))) in Controller.php line 76