mercredi 1 mars 2017

Laravel - Getting empty dataset when retrieving from DB

I am trying to populate a select dropdown but not seeing the values come through.

Controller:

public function create()
{
    $employees = Employee::get();

    $am = DB::table('employees')
        ->where('department', 'Account Manager')
        ->select('id', 'name')
        ->orderBy('name', 'asc')
        ->get();

    $pm = DB::table('employees')
        ->where('department', 'Project Manager')
        ->select('id', 'name')
        ->orderBy('name', 'asc')
        ->get();

    $employees->am = $am;
    $employees->pm = $pm;

    return view('projects/create', [
        'employees' => $employees
        ]);
}

View:

<div class="form-group">
                  <label>Account Manager</label>
                  <select class="form-control" name="am">
                    <option value=""></option>
                    @foreach ($employees as $employee)
                      <option value=""></option>
                    @endforeach
                  </select>
                </div>
                <div class="form-group">
                  <label>Project Manager</label>
                  <select class="form-control" name="pm">
                    <option value=""></option>
                    @foreach ($employees as $employee)
                      <option value=""></option>
                    @endforeach
                  </select>
                </div>

So, I know something is working because the dropdowns are showing there are 5 spaces which are the number of records in the DB but they are all blank. Doing a var_dump shows it to be NULL. Also, it is not taking into account the WHERE clause as both dropdowns show 5 spaces rather than 3 for Project Managers and 2 for Account Managers.

My Models do not have any relationship set up. Does it have to? Because I did a join for a different view and it works just fine.

My DB tables look like the following:

Projects:

id
name
am_id
pm_id

Employees:

id
name
department

Any ideas how to have the values populate in the dropdown?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire