zoukankan      html  css  js  c++  java
  • Laravel : Eloquent 新增

        
        public function ormCreate()
        {
            #  1. 使用模型新增 ->save()
            /* 
                $student = new Student();
                $student->name = '大圣';
                $student->age = 500;
                $student->sex = '猴';
                $student->save(); 
            */

            # 2. 使用模型的Create方法新增数据

            ## 2.1 报错,因为不允许批量赋值,需要在模型文件内 设置可以被批量赋值的属性 protected $fillable = ['name']; 。如果只设置name则只存name. 
            ##  protected $fillable = ['name','age','sex'];  设置要存储的属性
            /*  
                Student::create([
                    'name'=>'牛魔王','age'=>'600','sex'=>'牛'
                ]); 
            */
            
            # 3. firstOrCreate 用属性匹配内容,当结果不存在时自动创建并保存
            // Student::firstOrCreate(['name'=>'武卡卡']);

            # 4. firstOrNew    用属性匹配内容,当结果不存在时实例化一个新实例 ,需要手动报错
            Student::firstOrNew(['name'=>'武卡卡22222'])->save();
     
            
           $banner = Banner::firstOrNew([
                'title' => $request->title,
                'introduce' => $request->introduce,
                'img' => $fileName
            ])->save();

        }
  • 相关阅读:
    Ajax组件(wagang版)
    动画组件(wagang版)之基础篇:跳帧与延时
    Ajax组件(wagang版)之无依赖化
    ✍23 postgresql批量删除表
    ✍20 samba共享服务
    ✍16 asyncio异步IO
    ✍19 Tensonfow(GPU)使用
    ✍25 mysqlclient 安装(linux,mac)
    ✍17 aiohttp模块的使用
    ✍18 Linnux上安装node.js
  • 原文地址:https://www.cnblogs.com/500m/p/12789640.html
Copyright © 2011-2022 走看看