0

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

3
  • In Laravel, this would usually go in the controller, not the model. Take a look at Laravel's Query Builder. Commented May 31, 2017 at 23:45
  • "I want to write a select, insert and update query in my model that should work for any table in database." No, you don't. You want to use the built-in functionality instead of creating an additional weird ORM on top of the existing, well thought-out ORM. Commented May 31, 2017 at 23:49
  • Additionally, I strongly recommend subscribing to Laracasts. Specifically, here is a screencast based on the query builder - (Link updated) Commented Jun 1, 2017 at 0:04

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

if you really want something like that then you can have function there that accepts models as parameter:

public function create_record($model,$data)
{
    $model::create($data);
}

where in the controller you can do something like

$model = App\Fruit; // lets say fruit is a model you have
GeneralModel::create_record($model,$data);

but why not just go straight with it like:

$var = App\Fruit::create($data);

this is redundant .. for each model have a way to fetch , insert , update or delete records from their perspective table .. i suggest you read more documentation about Laravel Eloquent

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.