我正在尝试在Laravel WHERE IN
DB
查询中绑定数组
示例:
$arr = [1,2,3];
DB::select(DB::raw("select * from test1 WHERE id IN ? "), [$arr]);
由于某种原因,数组未更改为以下查询:
select * from test1 WHERE id IN (1,2,3)
有人知道我能以某种方式做到这一点吗?
答案 0 :(得分:4)
在laravel中尝试:
$arr = [1,2,3];
$result = DB::table('test1')->whereIn('id', $arr)->get();
dd($result);
并将此一个用于原始查询:
$arr = [1,2,3];
$arr = join(",",$arr);
$result = DB::select(DB::raw("SELECT * FROM test1 WHERE id IN (".$arr.")"));
dd($result);
为了防止sql注入你使用我在下面提到的东西。
$arr = [1,2];
$arr = join(",",$arr);
$result = DB::select(DB::raw("SELECT * FROM test1 WHERE id IN (?,?)"),$arr);
dd($result);
它将为你工作。
答案 1 :(得分:1)
或
class EnhancedTable extends Component {
state = {
selected: [],
data,
order: {
direction: 'asc',
by: 'deviceID',
},
search: '',
}
handleRequestSort = (event, property) => {
const orderBy = property
let order = 'desc'
if (this.state.order.by === property && this.state.order.direction === 'desc') {
order = 'asc'
}
const data = this.state.data.sort(
(a, b) => order === 'desc' ? b[orderBy] > a[orderBy] : a[orderBy] > b[orderBy],
)
this.setState({ data, order })
}
deleteSelected = () => {
const {data, selected} = this.state
this.setState({data: data.filter(item => !selected.includes(item)), selected: []})
}
handleSearch = event => {
const {data} = this.state
let filteredDatas = []
filteredDatas = data.filter(e => {
let mathedItems = Object.values(e)
let returnedItems
mathedItems.forEach(e => {
const regex = new RegExp(event.target.value, 'gi')
if (typeof e == 'string')
returnedItems = e.match(regex)
})
return returnedItems
})
this.setState({filterData: filteredDatas, search: event.target.value})
}
unselectSelected = () => {
this.setState({selected: []})
}
duplicate = (data, selected) => {
// const {data, selected} = this.state
this.setState({
// data: data.filter((item, index) => selected.includes(index)).reduce((p, c) => [...p, {...data[index]}], data),
// data : [...data, ...selected],
data : data,
selected: selected,
})
}
handleSelectChange = selected => {
this.setState({selected})
}
render = () => {
const {selected, data, search, order} = this.state
return (
<Paper>
<Table
data={data}
selectable
columns={columns}
order={order}
search={search}
selected={selected}
onSelect={this.handleSelectChange}
onDelete= {this.deleteSelected}
onSort={this.handleRequestSort}
onDuplicate={this.duplicate}
onSearch={this.handleSearch}
/>
</Paper>)
}
}
export default EnhancedTable
答案 2 :(得分:1)
$arr = [1,2,3];
$placeholders = implode(",", array_fill(0, count($arr), '?'));
DB::select("select * from test1 WHERE id IN ($placeholders)", $arr);
这个例子:
在这个例子中,我用等于数组长度的问号数量填充了一个新数组。然后我粘上新数组,用逗号分隔,并得到“?,?,?,......”。然后我在括号运算符“IN”之间插入这个子字符串。我将项目数组本身作为 select 函数的第二个参数传递。因此,元素数组的每个元素在“IN”运算符中都有自己的占位符。
答案 3 :(得分:0)
或Eloquent:
$q= TestModel::where('id',$arr)->get();
答案 4 :(得分:0)
尝试:
const updateDate = () => {
let date = new Date();
let month = date.getMonth() + 1;
if (month < 10) {
month = '0' + month;
};
let calenDay = date.getDate();
if (calenDay < 10) {
calenDay = '0' + calenDay;
}
return ({
date: `${date.getFullYear()}-${month}-${calenDay}`,
time: `${date.getHours()}:${date.getMinutes()}`
});
}
const confirmOrder = () => {
const date = updateDate();
axiosWithAuth()
.post(`/diner/${props.account.id}/confirm-order`, { ...date, ...orderData })
.then(res => {
console.log(res);
})
.catch(err => console.log(err));
}