想使用laravel将pdf文件上传到mysql

问题描述 投票:0回答:2

你好,我是Laravel新手。为了我的项目目的,我具有在数据库中上传pdf文件的功能。但数据未存储在数据库中。

这里是我尝试输入的代码=

controller-

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use DB;
class PostsController extends Controller
{
    public function index()
    {
        $id = Auth::id();
        $user = DB::table('users')->find($id);
        return view('posts', ['user' => $user]);
    }
    public function __construct()
    {
        $this->middleware('auth');
    }


    public function store()
    {
        //dd(request()->all());
        $data = request()->validate(['pcaption' => 'required']);
         Auth()->user()->posts()->create($data);
         dd(request()->all());

}
}

型号-

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class post extends Model
{
    protected $guarded = [];

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

}

来自localhost-的错误

Illuminate\Database\QueryException
SQLSTATE[HY000]: General error: 1364 Field 'post' doesn't have a default value (SQL: insert into `posts` (`pcaption`, `user_id`, `updated_at`, `created_at`) values (bh, 3, 2020-04-17 17:42:53, 2020-04-17 17:42:53))

这意味着帖子数据未保存在数据库中。我的错在哪里

php mysql file laravel-5
2个回答
0
投票

否,这意味着您的“ posts”表具有一个称为“ post”的列,并且该列没有默认值,并且不接受假定为null。

所以您可以这样解决此问题:

create table posts ( 
     ..., 
     post varchar(10000) DEFAULT '' not null,
     ...
);

0
投票

在“数据库表”后的列中,选择为Not Null。因此,您会收到此错误。在您的迁移文件中,您应该使用它来解决您的问题

public function up(){
    Schema::create('posts', function (Blueprint $table) {
        ....
        $table->text('post')->nullable();
        $table->timestamps();
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.