dongluo8439 2017-05-31 23:28
浏览 41

Laravel查询写作

I am developing a project in Laravel 5.4. I want to write a select, insert and update query in my model that should work for any table in database. I used to do this is Codeigniter and work fine there, but I don't know how to use it in Laravel.

Following is the code from a model file in Codeigniter

class General_Model extends CI_Model {

        public function fetch_CoustomQuery($sql){
        $query = $this->db->query($sql);
        return $query->result(); 

        }


        public function create_record($data, $tbl) 
        {
        $this->db->set($data);
        $this->db->insert($tbl);
        return ($this->db->affected_rows() != 1) ? FALSE : TRUE;
        }



        public function update_record($data, $tbl, $wher) 
        {
        $this->db->where($wher);
        $this->db->set($data);
        $this->db->update($tbl);
        }


        public function delete_record($tbl, $wher) 
        {
        $this->db->where($wher);
        $this->db->delete($tbl);         
        } 
}

It was very easy in Codeigniter. I only need to pass the parameters and worked fine. I want to write same queries in my model in Laravel. Please help

  • 写回答

2条回答 默认 最新

  • doujing5937 2017-05-31 23:46
    关注

    I would strongly recommend reading the documentation but the methods are as simple as:

    GeneralModel::create(["num" => 1, "name" => 2]);
    GeneralModel::where("num", ">", 2)->update(["num" => 1]);
    GeneralModel::where("num", ">", 2)->delete();
    

    There is no need to put these methods on the models.

    评论

报告相同问题?

悬赏问题

  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入