- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我尝试创建新订单时出现此错误:
"error": "array_merge(): 预期参数 1 为数组,给定为 null"
这里是我尝试用它创建新订单的整个函数,我尝试了 20 次,它工作正常,但一段时间后它停止工作并出现上述错误
public function placeOrder()
{
$user_id = auth()->id();
try {
$cart = Cart::where('client_id', '=', $user_id)->first();
$cartDetails = CartDetail::where('cart_id', '=', $cart->id)->get();
}catch (Exception $e){
return response()->json(["error" =>$e->getMessage()]);
}
try {
foreach ($cartDetails as $cartDetail){
$item = StoreItem::where('item_id', '=', $cartDetail->item_id)->first();
$this->items_total += $cartDetail->quantity;
$discountedPrice = ($item->unit_price * $item->discount) / 100;
$this->total_amount += $cartDetail->quantity * $discountedPrice;
}
}catch (Exception $e){
return response()->json(["error" =>$e->getMessage()]);
}
if(count($cartDetails->toArray()) == 0){
return response()->json(["warning" => "Please Add Cart Items"]);
}
$storeId = $cartDetails[0]->store_id;
try {
$store = Store::findOrFail($storeId);
$address = Address::where('user_id', '=', $user_id)->where('is_default', '=', true)->first();
}catch (Exception $e){
return response()->json(["error" => $e->getMessage()]);
}
$delivery_charge = 2;
$final_total = $this->total_amount + $delivery_charge;
$address_id = $address->id;
try {
$data = [
'items_total' => $this->items_total,
'total_amount' => $this->total_amount,
'is_paid' => false,
'delivery_charge' => $delivery_charge,
'shopper_earning' => $delivery_charge / 1.5,
'final_total' => $final_total,
'client_id' => $user_id,
'store_id' => $store->id,
'address_id' => $address_id,
'currency_id' => $store->currency_id
];
$createdOrder = Order::create($data);
if ($createdOrder){
foreach ($cartDetails as $cartDetail){
$cartDetail->delete();
}
event(new CartUpdated($cart->id, $user_id));
}
return response()->json(['success' => $createdOrder]);
}catch (Exception $e){
return response()->json(["error" =>$e->getMessage()]);
}
}
当我尝试调试它时,错误在 Order::create($data);
为什么会出现这个错误?
编辑:
调试控制台:
{
"message": "array_merge(): Expected parameter 1 to be an array, null given",
"exception": "ErrorException",
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastEvent.php",
"line": 78,
"trace": [
{
"function": "handleError",
"class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastEvent.php",
"line": 78,
"function": "array_merge"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastEvent.php",
"line": 64,
"function": "getPayloadFromEvent",
"class": "Illuminate\\Broadcasting\\BroadcastEvent",
"type": "->"
},
{
"function": "handle",
"class": "Illuminate\\Broadcasting\\BroadcastEvent",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php",
"line": 33,
"function": "call_user_func_array"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Container/Util.php",
"line": 36,
"function": "Illuminate\\Container\\{closure}",
"class": "Illuminate\\Container\\BoundMethod",
"type": "::"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php",
"line": 91,
"function": "unwrapIfClosure",
"class": "Illuminate\\Container\\Util",
"type": "::"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php",
"line": 35,
"function": "callBoundMethod",
"class": "Illuminate\\Container\\BoundMethod",
"type": "::"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Container/Container.php",
"line": 592,
"function": "call",
"class": "Illuminate\\Container\\BoundMethod",
"type": "::"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php",
"line": 94,
"function": "call",
"class": "Illuminate\\Container\\Container",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 128,
"function": "Illuminate\\Bus\\{closure}",
"class": "Illuminate\\Bus\\Dispatcher",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 103,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php",
"line": 98,
"function": "then",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php",
"line": 114,
"function": "dispatchNow",
"class": "Illuminate\\Bus\\Dispatcher",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php",
"line": 289,
"function": "queue",
"class": "Illuminate\\Broadcasting\\BroadcastManager",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php",
"line": 212,
"function": "broadcastEvent",
"class": "Illuminate\\Events\\Dispatcher",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Broadcasting/PendingBroadcast.php",
"line": 57,
"function": "dispatch",
"class": "Illuminate\\Events\\Dispatcher",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php",
"line": 218,
"function": "__destruct",
"class": "Illuminate\\Broadcasting\\PendingBroadcast",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/app/Observers/OrderStatusObserver.php",
"line": 15,
"function": "broadcast"
},
{
"function": "created",
"class": "App\\Observers\\OrderStatusObserver",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php",
"line": 388,
"function": "call_user_func_array"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php",
"line": 218,
"function": "Illuminate\\Events\\{closure}",
"class": "Illuminate\\Events\\Dispatcher",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php",
"line": 188,
"function": "dispatch",
"class": "Illuminate\\Events\\Dispatcher",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php",
"line": 886,
"function": "fireModelEvent",
"class": "Illuminate\\Database\\Eloquent\\Model",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php",
"line": 728,
"function": "performInsert",
"class": "Illuminate\\Database\\Eloquent\\Model",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php",
"line": 767,
"function": "save",
"class": "Illuminate\\Database\\Eloquent\\Model",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Support/helpers.php",
"line": 433,
"function": "Illuminate\\Database\\Eloquent\\{closure}",
"class": "Illuminate\\Database\\Eloquent\\Builder",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php",
"line": 768,
"function": "tap"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php",
"line": 23,
"function": "create",
"class": "Illuminate\\Database\\Eloquent\\Builder",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php",
"line": 1728,
"function": "forwardCallTo",
"class": "Illuminate\\Database\\Eloquent\\Model",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php",
"line": 1740,
"function": "__call",
"class": "Illuminate\\Database\\Eloquent\\Model",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/app/Http/Controllers/Api/OrderController.php",
"line": 108,
"function": "__callStatic",
"class": "Illuminate\\Database\\Eloquent\\Model",
"type": "::"
},
{
"function": "placeOrder",
"class": "App\\Http\\Controllers\\Api\\OrderController",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Routing/Controller.php",
"line": 54,
"function": "call_user_func_array"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php",
"line": 45,
"function": "callAction",
"class": "Illuminate\\Routing\\Controller",
"type": "->"
},
{
"file": "/home/sohaib/Desktop/Final-Project/Discounts/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
"line": 239,
"function": "dispatch",
"class": "Illuminate\\Routing\\ControllerDispatcher",
"type": "->"
},
这大约是一半,但我认为已经足够了,所以我在一个名为 OrderStatusObserver 的类之前创建了一个类,老实说,我不知道它是如何工作的,但这个类包含以下代码:
<?php
namespace App\Observers;
use App\Events\OrderStatus;
use App\Order;
class OrderStatusObserver
{
public function created(Order $order)
{
broadcast(new OrderStatus($order));
}
}
这是我在 OrderStatus 事件中的 broadcastWith 函数:
public function broadcastWith()
{
if($this->order->status == 'accepted'){
return ['current_position' => 2, 'id' => $this->order->id];
}elseif ($this->order->status == 'inprogress'){
return ['current_position' => 3];
}elseif ($this->order->status == 'onway'){
return ['current_position' => 4];
}elseif ($this->order->status == 'delivered'){
return ['current_position' => 5];
}
}
当我评论 broadcast(new OrderStatus($order));
这一行时它工作正常,所以任何人都知道这是什么问题,希望任何人都可以帮助我的英语
最佳答案
我认为您在创建方法中使用了数组 _merge()。你能给我们看看这个吗?您还可以在将 $data 发送到您的方法之前检查它是否不为空。
只是为了解释为什么我要求您共享您的 OrderStatus
类:根据您的错误跟踪,问题来自 BroadcastEvent.php
中的函数 getPayloadFromEvent
:
protected function getPayloadFromEvent($event)
{
if (method_exists($event, 'broadcastWith')) {
return array_merge(
$event->broadcastWith(), ['socket' => data_get($event, 'socket')]
);
}
$payload = [];
foreach ((new ReflectionClass($event))->getProperties(ReflectionProperty::IS_PUBLIC) as $property) {
$payload[$property->getName()] = $this->formatProperty($property->getValue($event));
}
unset($payload['broadcastQueue']);
return $payload;
}
恰好在 array_merge 调用中:
return array_merge($event->broadcastWith(), ['socket' => data_get($event, 'socket')]);
所以当您的错误消息显示 "array_merge(): Expected parameter 1 to be an array, null given"
时,您的 broadcastWith 返回 null 而不是数组。
关于php - "error": "array_merge(): Expected parameter 1 to be an array, null given",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62226015/
我想使用结构 DataResponse 作为 JSON() 的参数来响应用户。通过初始化 DataResponse 的实例,我得到了错误消息,给出了太多的参数,但给出了所有必要的参数。 type Da
我正在尝试将 google-one-tap 与本地主机上的 django 项目集成。所以我在 Client ID for Web 的 Authorized JavaScript origins 中添加
考虑一个类A,我如何编写一个具有与相同行为的模板 A& pretty(A& x) { /* make x pretty */ return x; } A pretty(A&& x) {
我正在使用 Hibernate envers 3.6.3.Final。我可以审核表,我可以看到 _audit 表中填充了 revision_number、revision_type 和实体数据。 我正
问题详细描述如下: 给定两个单词(beginWord 和 endWord)和字典的单词列表,找出是否存在从 beginWord 到 endWord 的转换序列,这样: 一次只能更改一个字母 每个转换后
我正在尝试解析任何选定的 mysql 表的单行的所有列字段和数据。 这背后的原因是为任何给定的单行创建一个类似“通用”的表解析器。 例如,我有这个表“tbl1”: +----+------------
我有一个列表,它可能包含也可能不包含重复的元素。给定另一个列表/元素集,我需要该列表中存在的所有唯一元素的列表。 Input: input_list = ['android', 'ios', 'and
需要编写一个算法来查找给定字符串在给定索引处的 Anagram,并按字典顺序排序。例如: Consider a String: ABC then all anagrams are in sorted
给定学生和铅笔的数量,假设学生有 154 名,铅笔有 93 名,如何用 Python 编写代码来获得比率。 输出:x:y 或者说给定两个数字的百分比并找出比率。 输出:x:y 最佳答案 import
给定学生和铅笔的数量,假设学生有 154 名,铅笔有 93 名,如何用 Python 编写代码来获得比率。 输出:x:y 或者说给定两个数字的百分比并找出比率。 输出:x:y 最佳答案 import
作为一名端到端自动化测试人员,我一直认为 Given、When、Then 语句(在使用 Cucumber 时合并到 Gherkin 语言中)应该只按 1.Given、2.When、3 的顺序出现.然后
我正在尝试以动态方式传递参数。我想使用 Perl 函数 given(){},但由于某种原因,我不能在其他任何东西中使用它。这就是我所拥有的。 print(given ($parity) { wh
我想在 cucumber 中测试以下功能。但是,我只想处理输入文件一次(以下功能中的@Given)。但是,它似乎每次都执行@Given 步骤。是否可以在以下功能中仅执行一次此@Given? @file
我想知道是否可以使用 given 参数来自 pytest 的 parametrize 函数。 示例: import pytest from hypothesis import given from h
在deep learning tutorials ,所有训练数据都存储在一个shared数组中,只有该数组的索引被传递给训练函数以切出一个小批量。我知道这允许将数据保留在 GPU 内存中,而不是将小块
我正在尝试运行以下代码: foreach my $k (keys %rec) { #switch for watchlist figures given ($k) { #line 93
我正在尝试在完全支持的情况下使用 GWT 规范,但是它的示例 official documentation有点简单。 在 SO 中搜索我发现了这个问题: Specs2 - How to define
我使用hypothesis 已经有一段时间了。我想知道如何重用 @given parts。 我有一些大约 20 行,我将整个 @given 部分复制到几个测试用例之上。 一个简单的测试例子 @give
我在运行 rspec 文件时不断收到错误: Failures:
让我们调用一个函数 function doSomethingAndInvokeCallback(callback){ // do something callback(); } 我可以
我是一名优秀的程序员,十分优秀!