laravel高级搜索

时间:2015-02-16 15:30:22

标签: php sql laravel

我想进行高级搜索,其中用户有可选参数进行搜索,我正在连接三个表中的数据,如下所示

$qry="SELECT rooms.*, salereservation.*, customers.*
                FROM rooms
                    JOIN salereservation
                        ON salereservation.room_id = rooms.room_id
                    JOIN customers
                        ON customers.id = salereservation.customer_id
                WHERE salereservation.sale_status=1 AND ";

我将可选参数附加到查询字符串,如下所示

if($fname!=''){
        $qry.="fname LIKE %fname% AND ";
    }
    if($lname!=''){
        $qry.="lname LIKE %:lname% AND ";
    }
    if($time_in!=''){
        $qry.="start_datetime LIKE %time_in% AND ";
    }
    if($time_out!=''){
        $qry.="end_datetime LIKE %time_out% AND ";
    }
    if($phone!=''){
        $qry.="phone LIKE %phone% AND ";
    }
    if($room_no!=''){
        $qry.="room_no LIKE %room_no%";
    }

我的问题是如何将代码转换为laravel查询构建器

2 个答案:

答案 0 :(得分:2)

我没有尝试这个代码,但它应该在laravel 4.2下工作。

$query = DB::table('rooms')
        ->join("salereservation", "salereservation.room_id", "=", "rooms.room_id")
        ->join("customers", "customers.id", "=", "salereservation.customer_id")
        ->where("salereservation.sale_status",'=',1)
        ->select('rooms.*', 'salereservation.*', 'customers.*');

if($fname!=''){
    $query->where("fname",'like',"%$fname%");
}

if($lname!=''){
    $query->where("lname",'like',"%$lname%");
}
if($time_in!=''){
   $query->where("start_datetime",'like',"%$time_in%");
}
if($time_out!=''){
    $query->where("end_datetime",'like',"%$time_out%");
}
if($phone!=''){
    $query->where("phone",'like',"%$phone%");
}
if($room_no!=''){
  $query->where("room_no",'like',"%$room_no%");
}
$data = $query->get(); //finally get the result

<强>更新

对于查询验证,您可以使用以下方式打印查询:

 $queries = DB::getQueryLog();
 $last_query = end($queries);
 dd($last_query);

并验证您的查询是否与您想要的查询不同。 如果情况有所不同,我们可以根据它们升级我们的查询结构。还可以使用laravel方法生成的最新查询进行更新。

但如果你仍然面临一些难以理解我的观点。让我知道。

答案 1 :(得分:0)

您必须像这样设置数据库:

rooms:
 - id
 - room_no

reservations:
 - customer_id
 - start_datetime
 - end_datatime
 - room_id

customers:
 - lname
 - fname
 - phone

然后在模型中设置propers关系

//Reservation model
class Reservation extends \Eloquent {

    public function room()
    {
        return $this->belongsTo('Room', 'room_id');
    }

    public function customer()
    {
        return $this->belongsTo('Customer', 'customer_id');
    }
}

创建如下的查询范围:

// Reservation model
// $terms is an array which pairs column_name to the querying value
public function scopeSearch($query, $terms)
{
    if ( ! empty($terms['room_no']))
    {
        $query->has('rooms.room_no', $terms['room_no']);
    }

    $customer_cols = ['lname', 'fname', 'phone'];
    $reservations_cols = ['start_datetime', 'end_datetime'];
    foreach ($terms as $key => $value)
    {
        if (in_array($key, $customer_cols))
        {
            $query->whereHas('customer', function($subquery) use ($key, $value)
            {
                $subquery->where($key, 'like', '%'.$value.'%');
            });
        }
        if (in_array($key, $reservation_cols))
        {
            $query->where($key, $value);
        }
    }

}

现在,您可以通过执行以下操作来调用查询范围:

$reservations = Reservation:search(array('fname' => 'value', 'lname' => 'value2', ...));

访问结果:

foreach ($reservations as $res)
{
    $res->room->number;
    $res->customer->name;
}