0

I'm new to PHP and I've tried to make the small test full-stack project. I have Vue.js app on the client (frontend) and PHP (Lumen) on the server (backend). The code looks as following:

Client:

Vue component:

async createPerson() {
  const optionAxios = {
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
  }
  try {
    await axios.post(`http://localhost:8000/api/persons/`, {
      firstName: 'Edward',
      lastName: 'Edwardson',
      address: 'Address8'
    }, optionAxios)
  } catch (error) {
    console.log(error)
  }
},

Server:

Router:

$router->group(['prefix' => 'api'], function () use ($router) {
  $router->post('persons', ['uses' => 'PersonController@create']);
});

Model:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Person extends Model
{
  protected $connection = 'mysql';
  protected $table = 'person';
  protected $primaryKey = 'id';
  public $incrementing = true;
  public $timestamps = false;

  protected $fillable = [
    'firstName', 'lastName', 'address'
  ];

  protected $hidden = [];
}

Controller:

<?php
namespace App\Http\Controllers;
use App\Person;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PersonController extends Controller
{
  public function __construct() {
    header("Access-Control-Allow-Origin: *");
  }

  public function create(Request $request)
  {
    $person = Person::create($request->all());
    error_log($person);
    return response()->json($person, 201);
  }
}

Database:

enter image description here

enter image description here

Debug session on server side - $request value:

enter image description here

The issue is the new record was added to database, but with default values that I set on database level. I'm not sure why the object I've passed on the client is not added.

{
    firstName: 'Edward',
    lastName: 'Edwardson',
    address: 'Address8'
}

And the last thing - it works if I use Postman. But, as you can see, it doesn't work with Axios.

tesicg
  • 3,971
  • 16
  • 62
  • 121
  • What do you get from the debug (`error_log`), and what gets returned to the front end? I assume by "default values that I set on the database", you mean all fields are set to "default"? – wally Aug 27 '21 at 12:39
  • 1
    By using error_log($request) I've got the correct object values. Yes, all values in database are set to "default". – tesicg Aug 27 '21 at 12:41
  • Just for full clarity - could you add a screenshot of what's been written into the database? (i.e. from the "Browse" tab in your SQL UI). – wally Aug 27 '21 at 12:48
  • 1
    I've just added the screen shot of the table. – tesicg Aug 27 '21 at 12:52
  • Stupid question - the debug log you've screenshotted shows the exact record written in with primary key `9` in the database screenshot ... is the `default` record definitely being created by the PHP debug shown in the other screenshot? – wally Aug 27 '21 at 12:56
  • One more thing - there is no migrations in my project. I've created database manually. – tesicg Aug 27 '21 at 12:57
  • The record with id = 9 was created by using Postman. – tesicg Aug 27 '21 at 13:00
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/236486/discussion-between-wally-and-tesicg). – wally Aug 27 '21 at 13:01

1 Answers1

1

Your problem is that you are changing what the content type of your request is. Do not write headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, as axios sends this as 'Content-Type': 'application/json', so when it arrives to Lumen, it "decodes" it correctly and you can do $request->all() and get any data you want. You don't even have to write any content-type header, it is all automatically done by axios in this case.

So your javascript code should be like:

async createPerson() {
  await axios.post('/api/persons/', {
    firstName: 'Edward',
    lastName: 'Edwardson',
    address: 'Address8'
  })
},
matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
  • I've added that header because of CORS issue. – tesicg Aug 31 '21 at 09:24
  • That should have nothing to do with CORs. If you have to add that, then your problem is in the `middleware` responsible for it (Lumen, not Javascript or Axios). – matiaslauriti Aug 31 '21 at 09:33
  • 2
    I've just removed the Axios options from the client and setup middleware on backend and everything works fine. Thank you! – tesicg Aug 31 '21 at 10:08