laravel增删查改实例

1.查
1)SalonQuestion::query()->orderBy('created_at', 'desc')->paginate(config('website.admin.page_size'));
2) SalonQuestion::find($id);
2.插入
SalonQuestion::insert([
            'salon_id' => $data['salon_id'],
            'title' => $data['title'],
            'description' => $data['description'],
            'answers_user_name' => $data['answers_user_name'],
            'answers_content' => $data['answers_content'],
            'amount' => setAmount(intval($data['amount'])),
            'status' => $data['status'],
            'created_at' => \Carbon\Carbon::now()
        ]);
注:批量插入时,将参数数组替换成多维数组即可
3.改
        $salon = SalonQuestion::find($id);
        if (!$salon) {
            return $this->error(route('admin.salonq.index'), '不存在,请核实');
        }
        $this->validate($request, $this->validateRules, $messages);
        $salon->salon_id = $request->input('salon_id');
        $salon->title = $request->input('title');
        $salon->description = $request->input('description');
        $salon->answers_user_name = $request->input('answers_user_name');
        $salon->answers_content = $request->input('answers_content');
        $salon->amount = setAmount(intval($request->input('amount')));
        $salon->status = $request->input('status');
        $salon->save();
        /**
        /*另一个更新方法
        */
        SalonQuestion::where('id','=';$id)->update(['salon_name'=>$salon_name])

4.删除
1)硬删
            $infos = SalonQuestion::find($id);
            if ($infos) {
                SalonQuestion::destroy($id);
            }
2)软删
use SoftDeletes;
数据库中要一个deleted_at
在迁移类中还要修改
5.关联两个表
在一个表的model中加一个方法类似如:   
 public function salon()
    {
        return $this->hasOne('App\Models\Salon','id','salon_id');
    }
SalonQuestion->Salon->salon_id;
6.查看一个sql的原语句
//对查询字段有要求的
WechatUser::select(DB::raw('DATE_FORMAT(created_at,\'%Y-%m-%d\') dates,count(*) as cunt'))
                ->where('created_at', '>', $labelTimes[0])
                ->where('created_at', '<', $labelTimes[7])
                ->groupBy('dates')->toSql();
7.laravel中获取刚刚插入的数据id
a.DB::insertGetId
b.save()方式插入   直接对象->id 就是新增id
8.laravel中连接查询实例
        /* 网站分类查询 */
        if (isset($filter['sort']) && $filter['sort']) {
                //这里的$filter用use关键字加上形式引入到内嵌方法的作用域中,这里将$filter定义为一个超全局变量也可以直接在内嵌方法作用域中使用
            $id = Relation::query()->join('categories', function($join) use ($filter){
                        $join->on('relations.belong_id', '=', 'categories.cate_id')->where('categories.cate_name', 'like', '%' . $filter['sort'] . '%');
                    })->get();
            foreach ($id as $v) {
                $web_id[] = $v->type_id;
            }
            $query->whereIn('web_id', $web_id);
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用IDEA、Spring Boot、JPA和MySQL进行增删查改的示例: 1. 首先,创建一个Spring Boot项目并添加JPA和MySQL依赖项。 2. 创建一个实体类,例如User,使用JPA注解来映射到数据库表。 ```java @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "name") private String name; @Column(name = "age") private Integer age; // 省略getter和setter方法 } ``` 3. 创建一个JPA Repository接口,例如UserRepository,继承自JpaRepository。 ```java public interface UserRepository extends JpaRepository<User, Long> { } ``` 4. 创建一个Controller类,例如UserController,使用@Autowired注解将UserRepository注入进来。 ```java @RestController @RequestMapping("/users") public class UserController { @Autowired private UserRepository userRepository; @GetMapping("") public List<User> getAllUsers() { return userRepository.findAll(); } @GetMapping("/{id}") public User getUserById(@PathVariable Long id) { return userRepository.findById(id).orElse(null); } @PostMapping("") public User createUser(@RequestBody User user) { return userRepository.save(user); } @PutMapping("/{id}") public User updateUser(@PathVariable Long id, @RequestBody User user) { User oldUser = userRepository.findById(id).orElse(null); if (oldUser != null) { oldUser.setName(user.getName()); oldUser.setAge(user.getAge()); return userRepository.save(oldUser); } return null; } @DeleteMapping("/{id}") public void deleteUser(@PathVariable Long id) { userRepository.deleteById(id); } } ``` 5. 在application.properties文件中配置MySQL数据库连接信息。 ``` spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.jpa.hibernate.ddl-auto=update ``` 6. 运行项目并使用Postman等工具测试增删查改接口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值