Home > Back-end >  My post method does not receive data in laravel
My post method does not receive data in laravel

Time:04-07

I wanted your help, because I create some routes in the controller with laravel --resource and when I receive the data by $request and insert it into my database, I do not receive data. And I forgot to mention that it is an api in laravel.

Here is my route code

    Route::resource('blog', App\Http\Controllers\BlogController::class)->only(['index','store','update','show','destroy']);

Here is my controller API code

    public function store(Request $request)
    {

        $blog = Blog::create($request->post());
        return response()->json([
            'blog'=>$blog
        ]);
    }

here I attach the capture after putting print_r($request->all()); look what appears to me Imsomnia Api Test

PLEASE HELP!

CodePudding user response:

You need to provide a little more information for SO members to help find the error and help with the solution.

I can point you to a few places in order to solve these issues

  1. Does the request inputs match the fields in the databases?
  2. Is there a validations rule that is not met and preventing the data from saving?
  3. Use a try and catch block to check the output.
  4. You can check the storage/log/laravel.log if there is an error that has been logged there.

CodePudding user response:

The error is from insomnia I guess.

We can check here using two approaches:

  1. On Insomnia you are sending JSON in the wrong format:

    {
     'key':'value'
    }
    

    Strings are not being used properly in JSON. Thus, you should send JSON this way.Check this out. Also, you can notice on your insomnia you are getting errors. Thus, because of invalid JSON it might not be sending the request body parameters.

    {
     "key":"value"
    }
    
  2. Try sending the request body using form-data or x-www-form-urlencoded.

  • Related