Reputation: 567
I am creating update feature using laravel, I have tried to passing value to be updated but the value is failed or wrong.
Here's my sql table
id | name | email | pic_for
1 Michael [email protected] null
2 John Doe [email protected] 1
3 Kelme [email protected] 2
4 Munich [email protected] 1
5 Phylon [email protected] 2
To show the value and changed it, I am using two statement in controller, users to get name user and pic for selected value
Here's my controller process
public function updateuser($id)
{
$users=DB::table('users')->where('id',$id)->get();
$pic=DB::table('users')->get();
//dd($id);
//dd($users);
//menampilkan data ke view index
return view('updateuser',['users'=>$users,'pic'=>$pic,'id'=>$id]);
}
Here's my view.blade
@foreach($users as $user)
<div class="row">
<input name="_method" type="hidden" value="PUT">
<div class="col-md-12 pr-1">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name" placeholder="Full Name" value="{{ $user->name }}">
</div>
</div>
<div class="col-md-12 pr-1">
<div class="form-group">
<label>In Charge</label>
<select name="in_charge" id="in_charge" >
@foreach($pic as $pics)
@if($pics->pic_for != null)
<option value="{{ $pics->pic_for}}" selected>{{ $pics->name }}</option>
@else
<option value="{{ $pics->id}}">{{ $pics->name }}</option>
@endif
@endforeach
</select>
</div>
</div>
@endforeach
What I want is when admin click data user, it's shown like the database
Name ; name
Pic : pic_for
And admin can change the pic column using select option value where the value is taken from all id.
The Problem is in selected value, when I click or show the page, the select option get wrong value
What I Want
Name : John Doe (id 2)
Pic For : Michael (id 1) selected value
It has same value with database
For Now
Name : John Doe (id 2)
Pic For : Phylon (id 5) selected value
Do you know where error code ?
Thank you
Upvotes: 2
Views: 615
Reputation: 1574
Change this line and try
@if($pics->id == $user->pic_for)
<option value="{{ $pics->pic_for}}" selected>{{ $pics->name }}</option>
@else
You need to check the pic_for from users
Upvotes: 2