我只打印部门名称

时间:2017-02-28 05:46:04

标签: laravel-5.2

$dept_name = DB::table('departments')->select('dept_name')->where('id', '=', $request->alloted_dept)->get();
strong text**dd ($dept_name);

我想只打印部门名称,但在浏览器中显示 输出:

array:1 [▼
  0 => {#269 ▼
    +"dept_name": "IPS department"
  }
]

在echo $ dept_name上输出

ErrorException in abcController.php line 34: Array to string conversion

我想只输出“IPS部门”。

2 个答案:

答案 0 :(得分:0)

你必须循环你的结果

$dept_name = DB::table('departments')->select('dept_name')->where('id', '=', $request->alloted_dept)->get();

控制器

foreach($dept_name as $name) {
   echo $name;
}

视图

@foreach($dept_name as $name)
   {{ $name }}
@endforeach

答案 1 :(得分:0)

我是这样做的:

$dept_name = DB::table('departments')->select('dept_name')->where('id', '=', $request->alloted_dept)->get();
$dept = "";
foreach($dept_name as $name) {
    $dept = $name->dept_name;
}
echo $dept;

输出:

IPS department

感谢您回答Saravanan Sampathkumar