I store the address of a user as json in my database.
$user->update([
'address' => json_encode([
'street_no' => $input['street_no'],
'street_name' => $input['street_name'],
'city' => $input['city']
])
]);
Now, I want to be able to view all this information in the updateView. So, my edit function passes the user information to the view like the following:
public function edit(){
$user = User::whereId(Auth::id())->first();
return view('profile/edit', compact('user'));
}
And inside the edit.blade.php I have the form:
{!! Form::model($user, ['method'=>'PUT', 'action'=> ['UserController@update', $user->id],'files'=>true]) !!}
<div class="form-group">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('email', 'Email:') !!}
{!! Form::text('email', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('passwrod', 'Passwrod:') !!}
{!! Form::text('passwrod', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('file', 'Profile Image:') !!}
{!! Form::file('image', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('phone', 'Phone:') !!}
{!! Form::text('phone', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('street_no', 'Street Number:') !!}
{!! Form::text('street_no', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('street_name', 'Street Name:') !!}
{!! Form::text('street_name', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('city', 'City:') !!}
{!! Form::text('city', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::submit('Save', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
However, the address fields like street_no, street_name and city show black.
How can I retrieve that information?
Use json_decode for that,
Controller
public function edit(){
$address = json_decode($user->address);
$user = User::whereId(Auth::id())->first();
return view('profile/edit', compact('user','address'));
}
View
$address will give you details in array, then you can use these data in view like this:
{!! Form::model($user, ['method'=>'PUT', 'action'=> ['UserController@update', $user->id],'files'=>true]) !!}
<div class="form-group">
{!! Form::label('name', 'Name:') !!}
{!! Form::text('name', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('email', 'Email:') !!}
{!! Form::text('email', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('passwrod', 'Passwrod:') !!}
{!! Form::text('passwrod', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('file', 'Profile Image:') !!}
{!! Form::file('image', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('phone', 'Phone:') !!}
{!! Form::text('phone', null, ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('street_no', 'Street Number:') !!}
{!! Form::text('street_no', $address['street_no'], ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('street_name', 'Street Name:') !!}
{!! Form::text('street_name', $address['street_name'], ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::label('city', 'City:') !!}
{!! Form::text('city', $address['city'], ['class'=>'form-control'])!!}
</div>
<div class="form-group">
{!! Form::submit('Save', ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
I haved added adress specific informations, please add other user info your self. I hope you understand.
First of all you model should cast the address
field as array: (See https://laravel.com/docs/5.8/eloquent-mutators )
class User extends Model
{
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'address' => 'array',
];
}
This way you don't need to json_encode
and json_decode
your data. Eloquent will handle this for you.
Now you can use dot notation for field names:
{!! Form::text('address.street_no', null, ['class'=>'form-control'])!!}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.