0

My dynamic drop down list is not working when I am trying to navigate to update page by using url id and then its showing errors.The result suppose to be a name of city that is associated with id in the database.

controller class

public function updateAddress($edit_id)
  { 
         $data=[];
         $data['edit_id']=$edit_id;
         $address_data = $this->address->find($edit_id);
         $data['first_name']=$address_data->first_name;
         $data['last_name']=$address_data->last_name;
         $data['street']=$address_data->street;
         $data['zip_code']=$address_data->zip_code;
         $data['city']=$address_data->city;


   return view('update/edit',$data);

  }

Here is my blade file. The rest of the fields are working properly except drop down list. I am using foreach loop to display data into the drop down list.

 <select id="city" name="city" >

 @foreach($data as $cities)

<option id="city" value="{{$cities->edit_id}}" selected="selected">{{$cities->city}}</option>

@endforeach

 </select>
Javier Larroulet
  • 3,047
  • 3
  • 13
  • 30
Yasir
  • 41
  • 1
  • 7
  • 1. How the form is being submitted, is it ajax or form submit? 2. What is the error? – Jesus Erwin Suarez Nov 28 '18 at 21:03
  • The error is "Undefined variable: data " when I clicked on the edit link to update the record by using url ID. It displays when edit page is trying to open. – Yasir Nov 28 '18 at 21:17
  • You're not passing any `data`-variable. You're passing `$data` to the view, but that will extract so every key in that array will exist as their own variables, like `$edit_id`, `$first_name` etc. If you want to pass the array as an array that you can iterate through, you need to do: `return view('update/edit',['data' => [$data]]);`. However, since you're only passing one set, the foreach seems a bit unnecessary. Just pass `['data' => $data]`. Then just do `$data['edit_id']` in your view, without the foreach. – M. Eriksson Nov 28 '18 at 21:28
  • You can read more about passing data to views [in the documentation](https://laravel.com/docs/5.7/views#passing-data-to-views) – M. Eriksson Nov 28 '18 at 21:33
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – miken32 Nov 28 '18 at 21:42
  • {{old('city')?old('city'):$city}} -- This code block working fine but it display only one value.I need to load other vlaues that are not associated with ID but if I want to update the record and want to select different city so It would not possible with out foreach.. – Yasir Nov 28 '18 at 21:47
  • Then you need to pass multiple cities. You're currently only passing one to the view. – M. Eriksson Nov 28 '18 at 21:54
  • so I am trying to pass with foreach but its still not working...Can you plz elaborate more? – Yasir Nov 28 '18 at 21:56

2 Answers2

0

Update the code in the controller so that:

return view('update/edit',$data);

Is changed to:

return view('update/edit', ['data' => $data]);
Niraj Shah
  • 15,087
  • 3
  • 41
  • 60
0

change your controller code to

return view('update/edit',compact('data'));

and change your view page like {{ $data['edit_id'] }}. you dont want to use @foreach.

Ismoil Shifoev
  • 5,512
  • 3
  • 24
  • 32
Amal S R
  • 870
  • 7
  • 21