简体   繁体   中英

write raw query for between in eloquent laravel

Hi I have a query scope in laravel and I have a column named km which is varchar . Now I want to write a raw query scope for it. The problem is it wont search for the varchar . So need to cast km to unsigned.

Query -:

Product::km($kmFrom, $kmTo);

Query scope

public function scopeKm($query, $kmFrom, $kmTo)
{   
    $kmFrom = (int)$kmFrom;
    $kmTo = (int)$kmTo;

    if((count($kmFrom) > 0) && count($kmTo) > 0){ 

        return $query->whereBetween('km', [$kmFrom, $kmTo]);   
    }
    return $query;
}

I want to write the db raw query for the above in which I cast the km column to unsigned in the raw query.

您可以编写原始查询,执行以下操作:

DB::select("SELECT * FROM table_name);

syntax for using raw query

 $result_1 = DB::table('tablename')->whereRaw("column name =".$variable)->select('name','improve','email',DB::raw("DATEDIFF('2016-11-10' , `age`) / 365.25 as age"),'age as dob')->first();

or if want complete raw then

DB::select("SELECT * FROM tablename where 1);

hope it will help you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM