killstreet
killstreet

Reputation: 1332

Dynamic query ORM Laravel 4.2

Alright I have a GeneralModel written in CodeIgniter and a friend asked me if I could convert it into Laravel 4.2 for him. I was working on this and I think I have most of it correct but I am getting stuck at the select statement.

In CodeIgniter I have the following:

public function getData($table, $multiple = 1, $field = FALSE, $val = FALSE){

    if($field != FALSE){
        // WHERE in case of FIELD / VAL :)
        $this->db->where($field, $val);
    }

    $query = $this->db->get($table);

    if($multiple == 1){
        // Multiple rows
        return $query->result_array();
    } else {
        // One row
        return $query->row_array();
    }
}

Does anyone here knows how I can convert this function into Laravel 4.2 syntax? I currently have:

public function getData($table, $multiple = 1, $field = FALSE, $val = FALSE){
    $result = DB::table($table);

}

I got stuck pretty quickly since I have no idea how I can achieve the same in Laravel 4.2 with splitting up the sections of the query like I did with CodeIgniter.

Upvotes: 0

Views: 169

Answers (2)

Jeff Lambert
Jeff Lambert

Reputation: 24661

Laravel is similar in that you can use the Fluent Query Builder to build your queries in multiple stages prior to actually making the query. Once you know this, the translation is pretty straightforward:

public function getData($table, $multiple = 1, $field = FALSE, $val = FALSE)
{
    $query = DB::table($table);

    if($field){
        // WHERE in case of FIELD / VAL :)
        $query = $query->where($field, $val);
    }

    if($multiple) {
        return $query->get();
    }

    return $query->first();
}

I don't think it's really good practice relying on Fluent though inside of an Eloquent model, but there are cases where that can't be helped. If the current objective is to convert the project to Laravel, there's probably calling code relying on the fact that this method exists. Converting the function to use Eloquent rather than Fluent will change the function's signature and cause other parts of the code to break, but it would look like this:

public function getData($multiple = true, $field = false, $val = false)
{
    $query = $this;

    if($field) {
        $query = $query->where($field, $val);
    }

    if($multiple) {
        return $query->get();
    }

    return $query->first();
}

The calling code itself can be modified to do the exact same function in Laravel like this:

// Instead of...
$result = $model->getData(1, 'field', 'value');

// You can do this:
$result = $model->where('field', 'value')->get();

// Or this if you'd rather not have multiple:
$result = $model->where('field', 'value')->first();

Using this function inside Eloquent (IMHO) in the long run doesn't really save you much, and instead is mostly just clutter.

Upvotes: 1

Iamzozo
Iamzozo

Reputation: 2358

You can chain methods in the same way:

public function getData($table, $multiple = 1, $field = FALSE, $val = FALSE)
{
    $query = DB::table($table);

    if ($field != FALSE) {
        // WHERE in case of FIELD / VAL :)
        $query->where($field, $val);
    }

    if ($multiple)
        return $query->get();
    else
        return $query->first();
}

Upvotes: 2

Related Questions