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

入门小弟请求指点!

《L05 电商实战》
从零开发一个电商项目,功能包括电商后台、商品 & SKU 管理、购物车、订单管理、支付宝支付、微信支付、订单退款流程、优惠券等
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
最佳答案

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

4年前
讨论数量: 5

看下你表里有哪些字段

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

数据表字段如下 file

4年前

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

4年前

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

4年前

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

4年前

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