【问题标题】:Laravel : Display the data in a tableLaravel:在表格中显示数据
【发布时间】:2016-01-26 13:50:41
【问题描述】:

我有一个表格,这个表格是给员工的,每个员工每年都有一笔付款,管理员应该填写数据并保存,以前的数据也应该填写在表格的正确位置,否则如果有没有数据,输入字段应该为空,数据应该是这样的:

但我得到的是:

我的密码: 控制器

$years = Year::with('employeeData')->get();

    $indicators = Indicator::all();
 return view('sustainability-data.input-data', compact('years', 'indicators'));

查看

@foreach($employees as $index=>$employee)
                    <tr>
                        <td>{{$employee->name}}</td>
                        @foreach($years as $year)
                            <td>
                                @foreach($year->employeeData as $datum)
                                        @if($datum->employee_id == $employee->id)
                                            {!! Form::text('row[$index][value]' ,$datum->value,['class' => 'form-control']) !!}
                                        @endif

                                @endforeach
                            </td>
                        @endforeach

在我失去理智之前,任何人都知道如何解决这个问题,我尝试了很多方法,但我没有解决这个问题

【问题讨论】:

    标签: php forms laravel-5.1


    【解决方案1】:

    看起来问题出在@foreach($year-&gt;employeeData as $datum) 的结构上。我假设如果员工没有给定年份的数据,那么@if($datum-&gt;employee_id == $employee-&gt;id) 将永远不会评估为true。如果是这种情况,那么输出表单字段的代码将不会运行。

    转换您当前拥有的最简单的方法是将@foreach 替换为以下内容:

    @if($year->emplyeeData()->where('employee_id', $employee->id)->count())
        {!! Form::text('row[$index][value]', $year->emplyeeData()->where('employee_id', $employee->id)->get()->value, ['class' => 'form-control']) !!}
    @else
        {!! Form::text('row[$index][value]', '', ['class' => 'form-control']) !!}
    @endif
    

    请注意,我对您的模型的设置方式做了一些猜测,因此可能需要修改此代码。

    我还会考虑从视图中删除此逻辑,并可能向您的 Employee 模型添加一个名为 getDataForYear 的方法,该方法返回值或空字符串。

    【讨论】:

    • 非常感谢,非常感谢您的帮助:D
    • 没问题。我也刚刚注意到'row[$index][value]' 需要用双引号引起来。当前版本不会用它的值替换$index
    • 是的,你是对的,我也注意到了,非常感谢:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2020-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多