流明:具有默认值的新记录已添加到数据库
Lumen: The new record with default values was added to database
我是 PHP 的新手,我尝试制作小型测试全栈项目。我在客户端(前端)上有 Vue.js 应用程序,在服务器(后端)上有 PHP (Lumen)。代码如下所示:
客户:
Vue 组件:
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)
}
},
服务器:
路由器:
$router->group(['prefix' => 'api'], function () use ($router) {
$router->post('persons', ['uses' => 'PersonController@create']);
});
型号:
<?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 = [];
}
控制器:
<?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);
}
}
数据库:
服务器端的调试会话 - $请求值:
问题是新记录已添加到数据库,但具有我在数据库级别设置的默认值。我不确定为什么没有添加我传递给客户端的对象。
{
firstName: 'Edward',
lastName: 'Edwardson',
address: 'Address8'
}
最后一件事 - 如果我使用 Postman,它就可以工作。但是,如您所见,它不适用于 Axios。
您的问题是您正在更改请求的 content type
。不要写 headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
,因为 axios
将它作为 'Content-Type': 'application/json'
发送,所以当它到达 Lumen 时,它会正确地“解码”它,你可以做 $request->all()
并获取任何数据你要。你甚至不必写任何 content-type
header,在这种情况下,它都是由 axios
自动完成的。
所以你的 javascript 代码应该是这样的:
async createPerson() {
await axios.post('/api/persons/', {
firstName: 'Edward',
lastName: 'Edwardson',
address: 'Address8'
})
},
我是 PHP 的新手,我尝试制作小型测试全栈项目。我在客户端(前端)上有 Vue.js 应用程序,在服务器(后端)上有 PHP (Lumen)。代码如下所示:
客户:
Vue 组件:
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)
}
},
服务器:
路由器:
$router->group(['prefix' => 'api'], function () use ($router) {
$router->post('persons', ['uses' => 'PersonController@create']);
});
型号:
<?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 = [];
}
控制器:
<?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);
}
}
数据库:
服务器端的调试会话 - $请求值:
问题是新记录已添加到数据库,但具有我在数据库级别设置的默认值。我不确定为什么没有添加我传递给客户端的对象。
{
firstName: 'Edward',
lastName: 'Edwardson',
address: 'Address8'
}
最后一件事 - 如果我使用 Postman,它就可以工作。但是,如您所见,它不适用于 Axios。
您的问题是您正在更改请求的 content type
。不要写 headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
,因为 axios
将它作为 'Content-Type': 'application/json'
发送,所以当它到达 Lumen 时,它会正确地“解码”它,你可以做 $request->all()
并获取任何数据你要。你甚至不必写任何 content-type
header,在这种情况下,它都是由 axios
自动完成的。
所以你的 javascript 代码应该是这样的:
async createPerson() {
await axios.post('/api/persons/', {
firstName: 'Edward',
lastName: 'Edwardson',
address: 'Address8'
})
},