php - Laravel:如何将参数传递到我的 View 并在 Form::text 和 Form::date 中显示它们?

标签 php laravel laravel-blade

我正在尝试制作用于编辑字段的 View 。

其实我有两个问题:

1) 我使用我的 Controller 从我的数据库中获取我的数据,这是有效的,我尝试将它传递给我的 View ,但那是行不通的...

2) 我想在 Form::text 和 Form::date 中显示这些数据,这不起作用...

我的 Controller 中有什么:

public function edit($id)
    {
        $data = DB::connection('my-db')
            ->table('my-table')
            ->where('id', '=', $id)
            ->select('field1', 'field2')
            ->first();

        return view('my-view', compact('field1', 'field2'));
    }

我什至不知道返回 View 中的紧凑是否像这样工作

我的观点:

<div class="col-md-6">
  {!! Form::text(field1, "", ['id'=> 'idField', 'class' => 'form-control', 'placeholder' => 'Modify field']) !!}
</div>
<div class="col-md-2">
  {!! Form::date(field2, "", ['id'=> 'datetimepicker', 'class' => 'form-control']) !!}
</div>

我希望它是可以理解的,感谢您以后的回答:)

最佳答案

public function edit($id)
{
    $data = DB::connection('my-db')
            ->table('my-table')
            ->where('id', '=', $id)
            ->select('field1', 'field2')
            ->first();

    $field1 = $data->field1; // intialize the $field1 variable 
    $field2 = $data->field2; // intialize the $field2 varialbe

    return view('my-view', compact('field1', 'field2'));
}

你在变量名前遗漏了 $ 符号

field1 更改为 $field1

<div class="col-md-6">
  {!! Form::text('field1', $field1, ['id'=> 'idField', 'class' => 'form-control', 'placeholder' => 'Modify field']) !!}
</div>
<div class="col-md-2">
  {!! Form::date('field2', $field2, ['id'=> 'datetimepicker', 'class' => 'form-control']) !!}
</div>

更多信息,您可以访问Laravel Form Collective Documentation

关于php - Laravel:如何将参数传递到我的 View 并在 Form::text 和 Form::date 中显示它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56885158/

相关文章:

php - 根据第三表行显示来自两个组合表的数据

javascript - file_put_contents() : failed to open stream

Laravel:1060 运行迁移时出现重复的列名称

php - Laravel 5 - 仅在授权后允许管理员注册

php - AWS EC2 Spot Instance PHP 在发出现货请求时添加标签

laravel - 如何通过 Laravel 的 Eloquent 关系和预加载连接 3 个表?

laravel-5 - 在 Laravel 5.4 的 Blade View 中插入一个配置值

php - Laravel - foreach 局部变量正在覆盖全局变量

php - 链接 css Assets 的 Laravel 5 问题

php - 如何将行从一个表复制到另一个具有不同列数的表