laravellaravelcollective

When trying to show a view (edit.blade.php) containing a Laravel Collective form, gives 'Invalid argument supplied for foreach()' - Laravel


When clicking the edit button in show.blade.php it should show edit.blade.php, but gives 'Invalid argument supplied for foreach()'

Everything else works... Any ideas what I'm doing wrong?

Whole error msg: ErrorException Invalid argument supplied for foreach() (View: Real-estate/resources/views/properties/edit.blade.php) (127.0.0.1:8000/properties/8/edit)

The foreach loop (in: vendor/laravelcollective/html/src/FormBuilder.php:656)

foreach ($list as $value => $display) {

     $optionAttributes = $optionsAttributes[$value] ?? [];

     $optgroupAttributes = $optgroupsAttributes[$value] ?? [];

     $html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes, $optgroupAttributes);
}

Views

show.blade.php

<a href="/properties/{{ $property->id }}/edit" class="btn btn-light">Edit</a>

edit.blade.php

{!! Form::open(['action' => ['PropertiesController@update', $property->id], 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}  
        // a bunch of <div class="form-group"></div> go here
        {{ Form::hidden('_method', 'PUT') }}
        {{ Form::submit('Done', ['class' => 'btn btn-primary']) }}
 {!! Form::close() !!}

create.blade.php

{!! Form::open(['action' => 'PropertiesController@store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
       // a bunch of <div class="form-group"></div> go here
       {{ Form::submit('Publish', ['class' => 'btn btn-primary']) }} 
{!! Form::close() !!}

Controller

public function store(Request $request)
{
    $property = new Property;
    $property->title = $request->input('title');
    .
    .
    .
    $property->save();
    return redirect('/home');
}

public function update(Request $request, $id)
{
    $property = Property::find($id);
    $property->title = $request->input('title');
    .
    .
    .
    $property->save();
    return redirect('/home');
}

dd($property) before $property->save(); in public function store() above

App\Property {#1204 ▼
  #table: "properties"
  +primaryKey: "id"
  #connection: null
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
  #attributes: array:10 [▼
    "title" => "Nice House"
    "reference_no" => "1234"
    "published_date" => "2021-06-04"
    "price" => "400k"
    "property_type" => "example"
    "area" => "example"
    "city" => "Example city"
    "description" => "example"
    "images" => "noimage.jpg"
    "user_id" => 2
  ]
  #original: []
  #changes: []
  #casts: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
  #guarded: array:1 [▼
    0 => "*"
  ]
}

Solution

  • Ok so the problem is now solved. The error was caused by a wrongly configured form field: {{ Form::select }} in create.blade.php. I was missing null, so it should be: {{ Form::select('city', ['Example City 1' => 'Example City 1', 'Example City 2' => 'Example City 2' ], null, ['placeholder' => 'Select city...']) }}