我正在尝试在视图中显示查询。我希望每个结果都成为一个提交按钮,单击该按钮将提交其值。
在 ListController 中,我有以下代码:
public function index()
{
$tables = DB::table('tables')->select('name')->get();
return view('welcome', compact('tables'));
}
在 welcome.blade.php 中,我有:
<form action="{{ route('get_table') }}" method="POST">
{{ csrf_field() }}
@foreach($tables as $data)
<input type="submit" name="tables[]" value="{{ $data }}">
@endforeach
</form>
在路线 web.php 中,我有:
Route::get('/', 'ListController@index');
我收到此错误:
"htmlspecialchars() expects parameter 1 to be string, object given (View:...welcome.blade.php)"
我在做什么错了?
答案 0 :(得分:1)
$data
是一个对象,尽管您只选择了一个列。您有两种选择:
为$data
使用name属性:
<input type="submit" name="tables[]" value="{{ $data->name }}">
或使用pluck
检索单个名称的集合。
$tables = DB::table('tables')->pluck('name');