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

        }
  • 相关阅读:
    JS——正则案例
    JS——正则
    JS——旋转木马
    JS——缓动框架的问题
    JS——缓慢动画封装案例
    JS——缓慢动画封装
    JS——隐式全局变量
    JS——样式获取的兼容写法
    JS——冒泡案例
    JS——事件冒泡与捕获
  • 原文地址:https://www.cnblogs.com/500m/p/12789640.html
Copyright © 2011-2022 走看看