Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel method not recieving post data on $request

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()

like image 374
Danny Sandi Avatar asked Jan 29 '16 20:01

Danny Sandi


2 Answers

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.

like image 66
Danny Sandi Avatar answered Oct 18 '22 05:10

Danny Sandi


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?

like image 2
Zoe Blair Avatar answered Oct 18 '22 04:10

Zoe Blair