簡體   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