mysql - Laravel DB 查询数组

标签 mysql arrays laravel object type-conversion

我将“hello,world”作为数组存储在 mysql 数据库中,然后尝试使用以下查询进行检索

 @php
    $post_id = $post->id;
    $result= DB::table('posts')
    ->select('tags')
    ->where('id', '=',  $post_id)
    ->get();

    echo($result);

 @endphp

回显结果为:

[{"tags":"hello,world"}] 

我需要为上面的结果回显单个值

hello 

world

最佳答案

@php
    $post_id = $post->id;
    $result= DB::table('posts')
    ->select('tags')
    ->where('id', '=',  $post_id)
    ->first();

  $tags = $results->tags;
  $tagsArray =  explode(',', $tags);
 @endphp

现在

@foreach($tagsArray as $ta)
{{ $ta }} <br />

@endforeach

你的输出将是

hello

world

关于mysql - Laravel DB 查询数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49318034/

相关文章:

php - 检查 PDO 准备()、执行()是否至少返回一行

mysql - mysql中更复杂的累积总和列

mysql - 如何将 ruby​​ on Rails 应用程序导入到另一台计算机

mysql - 获取值在 MySQL 值范围内的行数

c++ - 在数组中使用指针

c - c语言排序数组错误

laravel - 如何将 SimplePie 与 Laravel 结合使用

PHP PDOException 找不到驱动程序

php - 如何将数据库结果集合组织为层次结构?

c - 将 float 组传递给 (void*) 函数参数