当用户提交他们选择的图片时,我想知道为什么在提交表单时不将其发送到数据库。我相信我的Models和Controller有点问题,即在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));
}