在 Laravel 5.1 上,一个方法不接收发布数据。
这是我的方法,其中 $request 不存储邮寄发送的数据。
class ProjectCommentController extends Controller
{
public function store(Request $request,$projectId)
{
$this->validate($request, [
'description' => ['required'],
'status' => ['required'],
'profile_id' => ['required']
]);
$project = Project::findOrFail($projectId);
return $project->comments()->save(new Comment([
'description' => $request->input('description'),
'status' => $request->input('status'),
'profile_id' => $request->input('profile_id')
]));
}
}
这是我在测试中的调用方式:
public function testProjectCommentCreation()
{
$category = factory(\App\Category::class)->create();
$project = factory(\App\Project::class)->create([
"category_id" => $category->id
]);
$profile = factory(\App\Profile::class)->create();
$comment = factory(\App\Comment::class)->make([
"profile_id"=>$profile->id
]);
$this->post(route('api.projects.comments.store', ['projects' => $project->id]), $comment->jsonSerialize(), $this->jsonHeaders)
->seeInDatabase('comments', ['project_id'=>$project->id,'description'=>$comment->description])
->assertResponseOk();
}
这是 $comment->jsonSerialize() 存储的内容:
array(3) {
'description' =>
string(10) "zFG8bW7EIz"
'status' =>
string(6) "active"
'profile_id' =>
int(629)
}
这是我的路线:
Route::resource('projects.comments','ProjectCommentController',['only'=>['index','store']]);
我的方法从 URL 接收 $projectId 并且它正在工作但是请求是空的,没有我从 $comment->jsonSerialize() 发送的数据
最佳答案
这是通过删除标题解决的
Content-type: application/json
我仍然不确定为什么它不能使用该 header ,但这不是方法或测试的错误。
关于php - Laravel 方法不接收 $request 上的发布数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35093270/