【问题标题】:want to upload a pdf file into mysql using laravel想使用 laravel 将 pdf 文件上传到 mysql
【发布时间】:2020-04-17 17:47:01
【问题描述】:

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

这是我尝试的代码=

控制器-

<?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);
    }

}

来自本地主机的错误-

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))

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

【问题讨论】:

  • 你发送这个字段pcaption, user_id, updated_at, created_at,但是通过表格设置post字段不能为空,因为它没有默认值
  • 关于该 SQL 错误以及如何在此处解决它的很好的文章:ourcodeworld.com/articles/read/757/…
  • 我用默认值解决了它。但没有改进,数据库中没有保存路径

标签: php mysql file laravel-5


【解决方案1】:

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

public function up(){
    Schema::create('posts', function (Blueprint $table) {
        ....
        $table->text('post')->nullable();
        $table->timestamps();
    });
}

【讨论】:

    【解决方案2】:

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

    所以你可以这样解决这个问题:

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

    【讨论】:

    • 我也用默认值解决了这个问题。但它仍然没有奏效。数据库中没有保存路径
    猜你喜欢
    • 2015-04-30
    • 2016-08-11
    • 2011-08-17
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多