class Goods extends BaseModel { //类型转换 读取写入数组,保存到数据库为 json array 商品宣传图片列表,采用JSON数组格式 protected $casts = [ 'counter_price' => 'float', 'retail_price' => 'float', 'is_new' => 'boolean', 'is_hot' => 'boolean', 'gallery' => 'array', 'is_on_sale' => 'boolean' ]; } //方式一 $product = Product::query()->create([ 'title' => '水杯', 'category id' => 1, 'is_on sale' => 1, 'price' => '1200', 'attr' => ['高' => '10cm', '容积' => '200ml'] ]); //方式二 $ret = Product::query()->insert([ 'title' => '水杯', 'category id' => 1, 'is_on sale' => 1, 'price' => '1200', 'attr' => json_encode(['高' => '10cm', '容积' => '200ml']) ]); DB::table('products')->insert([]); //方式三 $product = new Product(); $product->fill([ 'title' => '水杯', 'category id' => 1, 'is_on sale' => 1, 'price' => '1200', 'attr' => ['高' => '10cm', '容积' => '200ml'] ]); $product->title = '水杯'; $product->category_id = 1; $product->is_on_sale = 1; $product->price = '1200'; $product->attr = ['高' => '10cm', '容积' => '200ml']; $product->save(); //返回成功 或失败 //查询 $product = Product::all(); $product = Product::query()->get(); $product = Product::query()->where('id', 1)->first(); //修改 $product = Product::query()->where('id', 1)->update(['title' => '水杯2']); $product = Product::where('category_id', 10)->where('is_active', 1)->first(); // 查找分类 ID 为 10 且状态为激活的产品 $product = Product::query()->find(1); $product->title = '水杯2'; $product->save(); //删除 $product = Product::query()->find(1); $product->delete(); $product->restore(); // 恢复删除的记录
评论(0)
暂无评论