繁体   English   中英

使用axios发出发布请求时,为什么会出现500服务器错误?

[英]Why am I getting 500 server error when making post request with axios?

用户提交自己选择的图片后,我想知道为什么表单提交后不会将其发送到数据库。 我相信我的Models和Controller有点PostPicturesController.php ,即在PostPicturesController.php文件中,我的方法链接可能已关闭。 如何解决此问题,并使用户选择的文件在提交表单后发送到数据库。

我得到的控制台错误是:

POST http://my_website.test/home 500 (Internal Server Error)
Here's the error Error: Request failed with status code 500

我在日志中出现错误:

[2019-09-15 17:21:20] local.ERROR: Call to undefined method 
Illuminate\Database\Eloquent\Relations\BelongsTo::postPictures() 
{"userId":5,"exception":"[object] (BadMethodCallException(code: 0): 
Call to undefined method 
Illuminate\\Database\\Eloquent\\Relations\\BelongsTo::postPictures()

这是PostPicturesController.php

<?php

namespace App\Http\Controllers;

use App\User;
use App\PostPictures;
use Illuminate\Http\Request;

class PostPicturesController extends Controller
{
    public function create(Request $request, PostPictures $postPicture) {
        $uploadPic = $request->user()->postPictures()->get();
        return response()->json($postPicture->with('user')->find($uploadPic->id));
    }
}

这是PostPictures.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PostPictures extends Model {
//    protected $fillable = ['body'];

    public function user() {
        return $this->belongsTo(User::class);
    }
}

这是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 = [
        'name', '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 postPictures() {
        return $this->hasMany(PostPictures::class);
    }
}

这是我使用axios在react.js文件中的发布请求:

submission() {
    let picUrl = this.state.previewImgURL;

    axios.post(`/home`, picUrl)
        .then((response) => console.log("Here's the response " + response))
        .catch(error => console.log("Here's the error " + error));
 }

使用此软件进行调试api链接,检查出现错误的行或文件,并在此处发布

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM