繁体   English   中英

如何将json数据传递到更新视图| 拉拉韦尔

[英]How can I pass the json data to an update view | Laravel

我在数据库中将用户的地址存储为json。

    $user->update([
        'address' => json_encode([
            'street_no' => $input['street_no'],
            'street_name' => $input['street_name'],
            'city' => $input['city']
            ])
    ]);

现在,我希望能够在updateView中查看所有这些信息。 因此,我的编辑功能将用户信息传递给视图,如下所示:

public function edit(){
    $user = User::whereId(Auth::id())->first();
    return view('profile/edit', compact('user'));
}

在edit.blade.php中,我具有以下形式:

{!! 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() !!}

但是,地址字段,例如street_no,street_name和city显示为黑色。

如何获取该信息?

为此使用json_decode,

控制者

public function edit(){
    $address = json_decode($user->address);
    $user = User::whereId(Auth::id())->first();
    return view('profile/edit', compact('user','address'));
}

视图

$ address将为您提供数组中的详细信息,然后可以在视图中使用这些数据,如下所示:

{!! 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() !!}

我已经添加了地址的特定信息,请添加其他用户信息。 我希望你明白。

首先,您应该建模为将address字段转换为数组:(请参阅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',
    ];
}

这样,您无需对数据进行json_encodejson_decode 雄辩的将为您处理。

现在,您可以将点符号用于字段名称:

{!! Form::text('address.street_no', null, ['class'=>'form-control'])!!}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM