0

I have a form to edit the administrators of a post.

This form has some radio buttons. Each radio button corresponds to a administrator of a a post, so the id of each radio button is the id of the admin in the administrators table.

When the submit button is clicked I want to update just the administrator with the id of the checked administrator/radio button.

For that it should be necessary to pass to the update method the id of the post and the id of the checked radio button (administrator) and then update only that administrator id.

Im passing the id of the post with success but the admin id not. It appears syntax error, unexpected '{'.

Edit admins form:

<form method="post" class="clearfix" action="{{route('admins.update', ['id' => $post->id], ['adminID' => $admin->id])}}" enctype="multipart/form-data">
    {{csrf_field()}}
    @foreach($administrators $admin)
          <div class="form-check">
            <input class="form-check-input" type="radio" name="radiobutton" id="{{$admin->id}}" value="">
            <label class="form-check-label" for="">
              {{$admin->first_name}}
            </label>
          </div>
    @endforeach
  <!-- below I have form fields like administrator name, email, etc -->
  </form>

// update admins routes

Route::get('post/edit/{id}/admins',    [ 'uses' => 'AdminController@edit', 'as'=>'admins.edit']);
Route::post('post/update/{id}/admins', [ 'uses' => 'AdminController@update', 'as'=>'admins.update']);

// Admin controller

class AdministratorController extends Controller
{
    public function edit($id)
    {
        $post = Post::find($id);
        $administrators = Administrator::where('post_id', $id)->get();

        return view('administrators.edit')
            ->with('post', $post)
            ->with('administrators', $administrators));
    }
    public function update(Request $request, $id, $adminID){

        $this->validate($request, [
            'name' => 'required|string',
            'email' => 'required|integer',
            '...' => '...',
        ]);
        $post = Post::find($id);
        $admin = Administrator::find($post->id);
        $administratorToUpdate = Administrator::find($adminID);

        $administratorToUpdate->name = $request->name;
        $administratorToUpdate->email = $request->email;

        ....
        $administratorToUpdate->save();

}   

// Admin model

   class Administrator extends Model
{
    protected $fillable = [
        'first_name', 'email', 'password', 'description', 'post_id'
    ];

    public function post(){
        return $this->belongsTo('App\Post');
    }
}

1 Answer 1

1

Edit the route from :

{{route('admins.update', ['id' => $post->id], ['adminID' => $admin->id])}}

to:

{{route('admins.update', ['id' => $post->id, 'adminID' => $admin->id])}}

Edit: make sure to iterate over the administrators before defining the route.

There are some other mistakes on the form like foreach($administrators as $admin) $admin will be not be available for the route above so tweak that.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but like that it appears undefined variable:admin.
thats because $admin does not exists out of the foreach. check edited answer.
Thanks but that way it will be created a form and other elements for each administrator and it should create just a radio button for each administrator.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.