0

I have two tables (City and Area). The city has many areas. So I have successfully added and deleted the data. But I am facing multiple errors while editing and updating. I have just started learning Laravel. I used a resource controller but encountered a problem with declaring routes, so I tried the name route also.

This error I have encountered: Trying to get property 'id' of non-object and indicates the form's route ID. which is $editArea->id. Here is my code.

web.php route:

Route::resource('area','Admin\AreaController');
 Route::get('area/edit/{id}','Admin\AreaController@edit')->name('area.edit');
 Route::post('area/update/{id}','Admin\AreaController@update')->name('area.update');

edit button: <a href="{{route('area.edit', $row->id)}}">Edit</a>

AreaController:

public function edit(Area $area, $id)

    {

        $editArea= Area::find($id);
        $cityno= City::orderBy('city_name', 'asc')->get();
        return view('admin.editArea', compact('editArea','cityno'));
    }
public function update(Request $request, Area $area, $id)
    {

        $editArea=Area::find($id);
        $editArea->city_id=$request->city_id;
        $editArea->area_name=$request->area_name;
        $editArea->save();

    }

edit form:

<form method="POST" action="{{route('area.update', $editArea->id)}}">
                @csrf
                @method('put')
                    <select name="city_id">
                        @foreach($cityno as $row)
                            <option value="{{$row->id}}" >{{ucwords($row->city_name)}}</option>
                            @endforeach
                        </select>
                <input type="text" name="area_name" value="{{$editArea->area_name}}">
                    <button type="submit">Update Area</button>
               </form>

Area model:

public function city(){
       return $this->belongsTo(City::class,'city_id');}

city model:

  public function area(){
        return $this->hasMany(Area::class); }

As I'm a new learner, I'm stuck in these files to find my wrong code.

halfer
  • 19,824
  • 17
  • 99
  • 186
Tusher
  • 23
  • 8
  • What are the errors? You can always use `php artisan route:list` to show a list of the routes and their names, your `Route::resource('area'...` should suffice, no need for additional routes. https://laravel.com/docs/7.x/controllers#resource-controllers also has an overview of Resource Controller routes – brombeer May 20 '20 at 09:01
  • I have updated the post with errors, please check again. At first, I have use the resource route. But for edit and deleting I couldn't add the route properly, that's why I tried this way. – Tusher May 20 '20 at 09:17

0 Answers0