这是我的查询
$TagDatas = TagModel::whereIn('TagId', array($BlogData->Tagged))->get();
$BlogData->Tagged
的值为 1,2,3
(不是数组而是字符)
这是我的模型
<?php
class TagModel extends Eloquent
{
protected $primaryKey = 'AutoID';
protected $table = 'tag';
然后我做
@foreach($TagDatas as $TagData)
{{ $TagData->TagName }}
@endforeach
它只显示标签Table中的第一个元素
即使我调试它也会显示我
select * from `tag` where `TagId` in (1,2,3)
我缺少什么?
最佳答案
如果您将一个字符串值传递给 wherein()
,当 Eloquent 准备其绑定(bind)时,它将被视为文字字符串,因此实际执行的语句将是
select * from `tag` where `TagId` in ('1,2,3')
您需要将每个值 1、2 和 3 作为单独的数组条目传递,以便它们被正确绑定(bind)
$TagDatas = TagModel::whereIn('TagId', explode(',', $BlogData->Tagged))
->get();
这样wherein会将这三个元素视为单独的数组元素,给出执行的查询
select * from `tag` where `TagId` in (1, 2, 3)
关于php - Laravel Eloquent 选择问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27647564/