I just started working on Laravel 5. I am sending json request to my controller. It works fine for simple json. Now I have a scenario where json will be something like this
我刚开始研究Laravel 5。我正在向我的控制器发送json请求。它对简单的json很有效。现在我有一个场景,json将是这样的。
{
"orders":[
{
"user_id":"1",
"product_id":"10"
},
{
"user_id":"1",
"product_id":"15"
},
]
}
It can get more complex with nested objects. Inside my controller what I have been doing is simply like
使用嵌套对象会变得更复杂。在我的控制器里,我一直在做的事情很简单
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class ordersController extends Controller
{
public function __construct( Request $request)
{
//$this->middleware('products.prodcutsList');
}
public function makeOrder(Request $request)
{
$order_data= $request->only('orders');
print_r($order_data);
}
}
response I get is looks like this
我得到的响应是这样的
Array
(
[orders] =>
)
Please guide how can I get this in an array.
请指导如何在数组中获取这个。
2 个解决方案
#1
1
I'm not sure that will resolve you problem but your json is not correct you have to remove extra comma in the end :
我不确定这是否能解决你的问题,但你的json是不正确的,最后你必须删除额外的逗号:
{
"orders":[
{
"user_id":"1",
"product_id":"10"
},
{
"user_id":"1",
"product_id":"15"
}, //remove this comma
_________^
]
}
Note : You can use the following pretty website to validate you JSON jsonformatter.
注意:您可以使用以下漂亮的网站来验证JSON jsonformatter。
Hope this helps.
希望这个有帮助。
#2
0
You send your data as form data. Use the php input wrapper:
您将数据作为表单数据发送。使用php输入包装器:
$input = file_get_contents('php://input'); // json
print_r(json_decode($input)); // decode to php array
#1
1
I'm not sure that will resolve you problem but your json is not correct you have to remove extra comma in the end :
我不确定这是否能解决你的问题,但你的json是不正确的,最后你必须删除额外的逗号:
{
"orders":[
{
"user_id":"1",
"product_id":"10"
},
{
"user_id":"1",
"product_id":"15"
}, //remove this comma
_________^
]
}
Note : You can use the following pretty website to validate you JSON jsonformatter.
注意:您可以使用以下漂亮的网站来验证JSON jsonformatter。
Hope this helps.
希望这个有帮助。
#2
0
You send your data as form data. Use the php input wrapper:
您将数据作为表单数据发送。使用php输入包装器:
$input = file_get_contents('php://input'); // json
print_r(json_decode($input)); // decode to php array