我如何在laravel query where子句中执行这些操作?
select * from table_samples where 1 = id
// basically i can do like where id = 1
select * from table_samples where 1000 > rate
// basically i can do like where rate < 1000
select * from table_samples where 'bogart' = name
// basically i can do like where name = "bogart"
我的观点是,如果你交换column
和value
它在mysql上的工作,但是如果我在laravel where子句中这样做它不起作用吗?
像
这样的东西TableSample::where('1','id')->first();
TableSample::where('1000','>','rate')->first();
TableSample::where('bogart','name')->first();
// throws an error undefined column name 1, 100, and bogart.
// I know laravel . These are the correct query
TableSample::where('id','1')->first();
TableSample::where('rate','<','1000')->first();
TableSample::where('name','bogart')->first();
laravel中是否有where子句可以接受或确定您是否尝试交换value
和column
?
答案 0 :(得分:1)
你可以使用DB::raw()
。
TableSample::whereRaw('? = id', [1])->first();
请确保使用上述参数替换来防止创建SQL注入漏洞。
答案 1 :(得分:0)
whereRaw
和orWhereRaw
方法可用于将raw where子句注入查询
TableSample::whereRaw('1 = id')->first();