Laravel insert方法无法插入数据到库(排除fillable设置)入门小弟请求指点

开发环境

  • Laravel v8.33.1 (PHP v7.4.3)
  • Mysql V8.0.12
class Products extends Model
{
    // use HasFactory;

    protected $fillable = [
        'number',
        'title',
        'squad_id',
        'station_id',
        'life',
        'critical',
        'capacity',
        'created_time',
        'updated_time',
        'squad_id',
        'note'
    ];
// ......

在文档和网络中找到大多是在Model 中设置 $fillable 字段,设置后依然无效

以下是待插入数据(print_r($data))

Array
(
    [0] => Array
        (
            [number] => DZ13241242720
            [title] => 右后踏板支架总成
            [squad_id] => 0
            [station_id] => 0
            [life] => 1
            [created_time] => 1616900199
            [updated_time] => 1616900199
        )

    [1] => Array
        (
            [number] => DZ13241440010
            [title] => 固定横梁
            [squad_id] => 0
            [station_id] => 0
            [life] => 1
            [created_time] => 1616900199
            [updated_time] => 1616900199
        )

)

使用业务如下

$productInsert = DB::table('products')->insert($data);
dd($productInsert);

控制台打印

Laravel v8.insert方法无法插入数据到库(排除fillable设置)
数据表如下设置,在执行sql语句是可以插入数据
Laravel v8.insert方法无法插入数据到库(排除fillable设置)
数据库配置文件如下

[mysql]
default-character-set=utf8mb4

[mysqld]
port=3306
default_authentication_plugin=mysql_native_password
basedir=D:/lancema/....文件路径略.../MySQL8.0.12/
datadir=D:/lancema/....文件路径略.../MySQL8.0.12/data/
character-set-server=utf8mb4
default-storage-engine=InnoDB
max_connections=1000
collation-server=utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
innodb_buffer_pool_size=64M
innodb_flush_log_at_trx_commit=1
innodb_lock_wait_timeout=120
innodb_log_buffer_size=4M
innodb_log_file_size=256M
interactive_timeout=120
join_buffer_size=2M
key_buffer_size=32M
log-error=D:/lancema/....文件路径略.../MySQL8.0.12/data
log_error_verbosity=1
max_allowed_packet=16M
max_heap_table_size=64M
myisam_max_sort_file_size=64G
myisam_sort_buffer_size=32M
read_buffer_size=512kb
read_rnd_buffer_size=4M
server_id=1
skip-external-locking=on
sort_buffer_size=256kb
table_open_cache=256
thread_cache_size=16
tmp_table_size=64M
wait_timeout=120

[client]
port=3306
default-character-set=utf8mb4

入门小弟请求指点!

《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
最佳答案

DB和model对象操作是2个东西,你要用orm的对象操作,配置才有用

5年前
讨论数量: 5

看下你表里有哪些字段

5年前 评论
tu6ge-php (作者) 5年前

数据表字段如下 file

5年前

Db::table()是有含数据表名前缀,DB::table('lma_products')

5年前

是否是使用了事务但是未提交

5年前

DB和model对象操作是2个东西,你要用orm的对象操作,配置才有用

5年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!