在Laravel 5.1上,方法无法记录发布数据。
这是我的方法,其中$ request不存储post发送的数据。
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()
答案 0 :(得分:5)
通过删除标题
解决了这个问题Content-type: application/json
我仍然不确定为什么它没有使用该标题,但它不是方法或测试的错误。
答案 1 :(得分:2)
这有用吗?
$this->post(route('api.projects.comments.store', ['projects' => $project->id]), $comment->toArray())
->seeInDatabase('comments', ['project_id'=>$project->id,'description'=>$comment->description])
->assertResponseOk();
如果您在商店方法中放置dd($request->all());
,是否会显示任何内容?