Laravel通过控制器数据库查询来查看

时间:2018-07-10 15:53:32

标签: php sql laravel laravel-5

我正在尝试在视图中显示查询。我希望每个结果都成为一个提交按钮,单击该按钮将提交其值。

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)"

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

$data是一个对象,尽管您只选择了一个列。您有两种选择:

$data使用name属性:

<input type="submit" name="tables[]" value="{{ $data->name }}">

或使用pluck检索单个名称的集合。

$tables = DB::table('tables')->pluck('name');