php - Laravel Eloquent 选择问题

标签 php mysql laravel laravel-4 eloquent

这是我的查询

$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/

相关文章:

php - jpgraph textint y 轴返回 0f 而不是数字

php - 使用 Laravel 将 CSV 文件上传到 MySQL

php - 在 HTML 中包含 PHP

php - 仅在一个 WHILE 循环结果上显示 div

php - 拉维尔 5 : Controller Testing - PHPUnit returns green

laravel - 查询created_at日期仅等于laravel中的carbon::now()日期

php - 将数组保存到数据库

php - 从表中检索不同的值

java - 组合框依赖于另一个组合框 - JavaFX

laravel - Arduino POST 请求到 Laravel API