空数组laravel 5

时间:2017-04-28 08:37:36

标签: php laravel laravel-5

研究员,

这可能是一个愚蠢的问题,但我真的在我的申请中坚持这一部分。

我正在为公司制作一个小时注册系统,我是一名实习生。我所做的是在我的应用程序中创建一个删除按钮,只需单击一个简单的按钮即可删除记录。然而我所做的是删除我创建的表格中显示所有注册的所有可见记录。

当我删除最终记录时,我收到了“未定义索引时间注册”的错误。

public function index()
{
    $hoursregistrations = Hoursregistration::latest()->get();
    $user_id = Sentinel::getUser();
    $project_id = Project::pluck('description', 'id');
    $company_name = Company::where('status','client')->pluck('company_name', 'id');
    $activity_name = Subproject::pluck('id');
    //dd($hoursregistrations);

    return view('hoursregistrations.index', compact('hoursregistrations', 'user_id', 'project_id',
    'activity_name', 'company_name'));

}

我认为问题在于

$hoursregistrations = Hoursregistration::latest()->get();

因为我试图获得最新的注册价值,但没有权利?

现在我想知道如何在不破坏应用程序的插入和/或查看部分的情况下仍能显示我的视图。

 @foreach ($hoursregistrations as $hoursregistration)
                <tr>
                   <td hidden>{{  $user_id->first_name }}</td>
                   <td >{!! App\Project::getCompanyName($hoursregistration->project_id) !!} - {!! \App\Subproject::getTaskTitle($hoursregistration->subproject_id)!!}</td>      
                   <td>{!! $hoursregistration->note !!}</td>    
                   <td>{!! \App\Helpers::dateFormat($hoursregistration->date) !!}</td>
                   <td>{!! $hoursregistration->hours !!}</td>
                   <td>

                    <button id="btn-edit" name="btn-edit" class="btn btn-warning btn-xs btn-detail open-modal" value="{{$hoursregistration->id}}">Edit</button>
                    <form action="hoursregistrations/{{ $hoursregistration->id }}/delete" method="POST">
                        {{ csrf_field() }}
                        {{ method_field('DELETE') }}
                        <button id="btn-delete" type="submit" name="btn-delete" class="btn btn-danger btn-xs btn-delete delete-hoursregistration" value="{{ $hoursregistration->id }}">Delete</button>
                    </form>
                </td>
            </tr>
            @endforeach

这是foreach循环,显示index.blade.php视图中的数据

我很乐意帮忙,所以我可以继续完成这个应用程序

public function index()
{
    $hoursregistrations = Hoursregistration::latest()->get();
    $user_id = Sentinel::getUser();
    $project_id = Project::pluck('description', 'id');
    $company_name = Company::where('status','client')->pluck('company_name', 'id');
    $activity_name = Subproject::pluck('id');
    //dd($hoursregistrations);

    return view('hoursregistrations.index', compact('hoursregistrations', 'user_id', 'project_id',
    'activity_name', 'company_name'));

}

2 个答案:

答案 0 :(得分:0)

一切都是s或不是s

您所说的错误是因为您在$hoursregistration视图之前或之后出现s(没有@foreach)。解决方案是检查并删除循环外$hoursregistration的出现位置:

// You can't use $hoursregistration before
@foreach ($hoursregistrations as $hoursregistration)
    {{-- ... do what you want with $hoursregistration --}}
@endforeach
// You can't use $hoursregistration after

我确信$hoursregistration$hoursregistrations$hourregistration之间存在混淆(拼写错误)(甚至在你的帖子VS评论中也存在混淆)

答案 1 :(得分:0)

如果存在$ hoursregistrations,则可以使用此处,然后填充数据,否则不会发生任何事情

@if($hoursregistrations)
@foreach ($hoursregistrations as $hoursregistration)
                <tr>
                   <td hidden>{{  $user_id->first_name }}</td>
                   <td >{!! App\Project::getCompanyName($hoursregistration->project_id) !!} - {!! \App\Subproject::getTaskTitle($hoursregistration->subproject_id)!!}</td>      
                   <td>{!! $hoursregistration->note !!}</td>    
                   <td>{!! \App\Helpers::dateFormat($hoursregistration->date) !!}</td>
                   <td>{!! $hoursregistration->hours !!}</td>
                   <td>

                    <button id="btn-edit" name="btn-edit" class="btn btn-warning btn-xs btn-detail open-modal" value="{{$hoursregistration->id}}">Edit</button>
                    <form action="hoursregistrations/{{ $hoursregistration->id }}/delete" method="POST">
                        {{ csrf_field() }}
                        {{ method_field('DELETE') }}
                        <button id="btn-delete" type="submit" name="btn-delete" class="btn btn-danger btn-xs btn-delete delete-hoursregistration" value="{{ $hoursregistration->id }}">Delete</button>
                    </form>
                </td>
            </tr>
            @endforeach
@endif

我认为这可能会有所帮助