【问题标题】:Why won't the username appear in my payload when making a post request?为什么在发出 post 请求时用户名不会出现在我的有效负载中?
【发布时间】:2019-09-20 10:51:06
【问题描述】:

我正在尝试查看 usernameemail 列,它们是我的有效负载中的 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


    【解决方案1】:

    您错过了return 键:)。

    public function user()
    {
        return $this->belongsTo('App\User');
        ^^^^^^
    }
    

    【讨论】:

    • 我在开始使用 Laravel 时总是遇到这个问题。好吧,我仍然偶尔会发生一次,但不像以前那么频繁了,哈哈。
    • 哇...我觉得自己像个白痴,我被困了这么久。谢谢!
    【解决方案2】:

    我相信你没有正确传递外键,

    protected $fillable = ['body', 'user_id'];
    
    public function user()
    {
        $this->belongsTo('App\User', 'user_id');
    }
    

    【讨论】:

    • 试过了,还是遇到同样的问题:(
    • 你能做一个 dd 或 print_r($request->body);在你的 PostController 中?
    • 刚刚在 PostController 中运行 print_r($request-&gt;body);,这就是我得到的:{data: "some info{"body":"some info","user_id":1,"updated_…8:56","created_at":"2019-05-01 23:18:56","id":73}", status: 200, statusText: "OK", headers: {…}, config: {…}, …} 。我只是不明白我怎么看不到usernameemail,这没有任何意义。
    • 尝试在发出响应之前调用print_r($createdPost);
    • 得到了同样的回应
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多