vendredi 28 avril 2017

undefined index on empty array laravel 5

Fellow coders,

It might be a stupid question but I really am stuck on this part in my application.

I am making an hourregistration system for the company i'm an intern at. What I have done is creating a delete button in my application that deletes a record with the click of a simple button. Yet what I did was to delete all the visible records in the table I created that shows all the registrations.

When I deleted the final record I got an error of "undefined index hourregistration".

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'));

}

I think the problem lies at

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

Because I'm trying to get the latest value of the registration but there is none right?

Now i'm wondering how I could still show my view without breaking my inserting and/or viewing portion of the app.

 @foreach ($hoursregistrations as $hoursregistration)
                <tr>
                   <td hidden></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="">Edit</button>
                    <form action="hoursregistrations//delete" method="POST">
                        
                        
                        <button id="btn-delete" type="submit" name="btn-delete" class="btn btn-danger btn-xs btn-delete delete-hoursregistration" value="">Delete</button>
                    </form>
                </td>
            </tr>
            @endforeach

This is the foreach loop that shows the data in the index.blade.php view

I would love some help so I can continue finishing this application



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire