2

I am beginner in larval and I want to edit and update checkbox values. Now I can able to store checkbox values, but when I click edit it's not showing checked values in edit view. So please help me how can I resolve this solution. Here I will give you my code.

My Blade File

<input type="checkbox" name="hobbies" id="readbooks" value="Readbooks" {{ $users->hobbies=="Readbooks"? 'checked':'' }}>Readbooks
<input type="checkbox" name="hobbies" id="music"  value="Music"{{ $users->hobbies=="Music"? 'checked':'' }}/> Music
<input type="checkbox" name="hobbies" id="games" value="Games" {{ $users->hobbies=="Games"? 'checked':'' }}/> Games
                   

My controller File

 public function edit($id)
{
    $users = User::find($id);
    echo $users;
    return view('edit',['users'=>$users]);
}
 public function updateUser(Request $request, $id)
{ 
    User::whereId($id)->update($validatedData);
    return redirect("view")->withSuccess('User! Successfully Updated!');
}
9
  • I need to fetch checkbox values in edit view before update Commented Nov 14, 2021 at 17:19
  • $users->hobbies is an array if I am not wrong and you're simply comparing with a single string word which is failing the condition. You should use in_array("Music", $users->hobbies) instead of users->hobbies=="Music" and so on. Commented Nov 14, 2021 at 17:21
  • @OMiShah now that you mention it, it can be! And if that a comma seperated string you can use since php8 str_contains($users->hobbies, 'Music'). But only Karthika knows what it is ..!? ;-) Commented Nov 14, 2021 at 17:29
  • 1
    You mean like this? {{ in_array("Music", $users->hobbies)? 'checked':'' }}/> Music Commented Nov 14, 2021 at 17:30
  • @Karthika yeah........ if it is an array. Commented Nov 14, 2021 at 17:30

2 Answers 2

3

I think you need to give the hobbies as array back to the view and then check if the value of the input is in the array of hobbies:

First return the user to the view:

public function edit($id)
{
    $users = User::find($id);

    return view('edit', [
        'users' => $users,
        'hobbies' => explode(',', $user->hobbies)
    ]);
}

public function updateUser(Request $request, $id)
{ 
    User::find($id)->update($request->all());

    return redirect("view")->withSuccess('User Successfully Updated!');
}

Then in your view

<form action="{{ url('updateText/'.$users->id) }}" enctype="multipart/form-data" method="POST">
    <input type="checkbox" name="hobbies[]" id="readbooks" value="Readbooks" {{ in_array('Readbooks', $hobbies) ? 'checked' : '' }}>Readbooks
    <input type="checkbox" name="hobbies[]" id="music" value="Music" {{ in_array('Music', $hobbies) ? 'checked' : '' }}>Music
    <input type="checkbox" name="hobbies[]" id="games" value="Games" {{ in_array('Games', $hobbies) ? 'checked' : '' }}>Games
</form>
Sign up to request clarification or add additional context in comments.

8 Comments

I am getting route error Undefined variable: users This line sowing error <form action="{{ url('updateText/'.$users->id) }}" enctype="multipart/form-data" method="POST" >
@Karthika yeah, the variable is $user, because you are fetching one user
I am not getting clearly. Getting little bit confused
I've updated my answer with the <form> tag, just check how the $user variable is spelled. Use the singular word of user. I think this will help you
If i change user I will affect other fields
|
0

The code you have looks fine. but as @OMiShah correctly pointed out, the values may have been stored in an array or in a comma-separated string in the database. since we don't know this now, two possibilities to build the condition:

in_array("Music", $users->hobbies)

or

str_contains($users->hobbies, 'Music')

2 Comments

I tried like this {{ str_contains($users->hobbies, 'Music') }} but nothing response
@Karthika okay. Can you show us what dd($user->hobbies)` printed?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.