On Laravel 5.1 a method is not recivieng the post data.
This is my method where $request does not store the data sent by 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')
]));
}
}
This is how I call it from my test:
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();
}
This is what $comment->jsonSerialize() stores:
array(3) {
'description' =>
string(10) "zFG8bW7EIz"
'status' =>
string(6) "active"
'profile_id' =>
int(629)
}
And this is my route:
Route::resource('projects.comments','ProjectCommentController',['only'=>['index','store']]);
My method recieves $projectId from the URL and that is working but the request comes empty, without the data I send from $comment->jsonSerialize()
This was solved by removing the header
Content-type: application/json
I'm still not sure why it is not working with that header but it was not an error on the method or the test.
Does this work?
$this->post(route('api.projects.comments.store', ['projects' => $project->id]), $comment->toArray())
->seeInDatabase('comments', ['project_id'=>$project->id,'description'=>$comment->description])
->assertResponseOk();
Does anything show if you put dd($request->all());
in your store method?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With