【发布时间】:2019-09-20 10:51:06
【问题描述】:
我正在尝试查看 username 和 email 列,它们是我的有效负载中的 SQL db 列,但由于某种原因它没有显示。我使用 React 作为前端,使用 Laravel 作为后端,出于某种原因,我没有看到它们。
我可能做错了什么?
这是User.php 模型:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'username', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function posts() {
return $this->hasMany(Post::class);
}
}
这是Posts.php 模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = ['body'];
public function user() {
$this->belongsTo(User::class);
}
}
这里是PostController.php:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class PostController extends Controller
{
public function create(Request $request, Post $post) {
// create post
$createdPost = $request->user()->posts()->create([
'body' => $request->body
]);
// return response
return response()->json($createdPost);
}
}
这里是web.php:
Auth::routes();
Route::group(['middleware' => ['auth']], function () {
Route::post('/posts', 'PostController@create');
});
这是我的发帖请求:
constructor(props) {
super(props);
this.state = {
body: '',
posts: []
};
this.handleSubmit = this.handleSubmit.bind(this);
this.handleChange = this.handleChange.bind(this);
}
handleSubmit(e) {
e.preventDefault();
// this.postData();
axios.post('/posts', {
body: this.state.body
}).then(response => {
console.log(response);
this.setState({
posts: [...this.state.posts, response.data]
});
});
this.setState({
body: ''
});
}
【问题讨论】:
标签: php reactjs laravel debugging axios