phplaravellaravel-bladelaravelcollective

Laravel 5.7 how can I populate a form::select from my controller


I have seen similar questions and their answers, but none of them have worked so far for my case. I guess this issue is something simple, but I cannot find the solution for I have tried several ways to populate my dropdown list to no avail. This is my controller:

    namespace MIS\Http\Controllers;

    use MIS\Http\Controllers\Controller;
    use Illuminate\Http\Request;
    use Illuminate\Support\Facades\DB;
    use Illuminate\Support\Facades\View;
    use Response;

    class pagesController extends Controller
    {

       public function provinces()
       {
          $data = DB::table('provinces')->select('province_code', 'province_name_english')->get();
          return view('pages/provinces', 'data');
       }
    }

This is my view code:

   <div class="content">
      {!! Form::open(['url' => '']) !!}
      <div class="title m-b-md">Address</div>
      {!! Form::select("provinceList", $data, "Siemreab", ['class' => 'form-control']) !!}
      <select id="districtList"></select><br />
      <select id="communeList"></select><br />
      <select id="villageList"></select><br />
      {!! Form::close() !!}
   </div>

This keeps giving me errors like: - array_merge(): Argument #2 is not an array (for that controller/action)

If I change my method to this:

return view('pages/provinces')->with('data', $data); 

I get this error:

htmlspecialchars() expects parameter 1 to be string, object given (View: D:\web\app\MIS\resources\views\pages\provinces.blade.php)

To mention only two of the different ways I have tested so far. I seems there is no way for the view to receive an array from the controller.

Any help is appreciated.


Solution

  • You can adjust the controller to pass the data with a more descriptive name and in the correct format for Form::select(...):

    namespace MIS\Http\Controllers;
    
    use Illuminate\Support\Facades\DB;
    
    class pagesController extends Controller
    {
    
       public function provinces()
       {
          // key by province_code
          $provinces = DB::table('provinces')
              ->pluck('province_name_english', 'province_code')
              ->toArray(); // if you want an array
    
          return view('pages.provinces', [
              'provinces' => $provinces,
              'default_province_code' => ...,
          ]);
       }
    }
    

    Your view can use $provinces as the list keyed by 'province_code':

    <div class="content">
        {!! Form::open(['url' => 'somewhere']) !!}
        <div class="title m-b-md">Address</div>
        {!! Form::select("provinceList", $provinces, $default_province_code, ['class' => 'form-control']) !!}
        <select id="districtList"></select><br />
        <select id="communeList"></select><br />
        <select id="villageList"></select><br />
        {!! Form::close() !!}
    </div>
    

    Laravel 6.x Docs - Views - Passing Data to Views view()

    Laravel 6.x Docs - Database - Query Builder - Retrieving Results - Retrieving A List Of Column Values pluck()

    LaravelCollective Docs - HTML 6.x - Drop-Down Lists Form::select()