vendredi 6 juillet 2018

LARAVEL 5.5: Fetch unique values from database and pass to view as form select options

I'm looking to fetch unique values from a table in my database and pass the company name into my view as options in a drop down list within a form.

The function in my controller looks like this:

public function index()
{
    $id = Auth::user()->id;
    $admin = Admin::find($id);
    $companies = DB::table('vacancies')->distinct()->select('company')->get()->toArray();
    return view('admin')->with('admin',$admin)->with('companies',$companies);
}

And my view looks like this:

<div class="form-group">
   @foreach ($companies as $company)
     
     
   @endforeach
</div>

I'm getting the following error:

Invalid argument supplied for foreach()

If I try I can see an array is being passed through to the view OK. This array looks like this:

array:353[
  0 => {#274 ▼
  +"company": "Example company name"
  }......
]

Ditching the loop and reverting back to:

<div class="form-group">
  
  
</div>

causes another error to be thrown which states:

htmlspecialchars() expects parameter 1 to be string, object given

Where am I going wrong here?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire