0

我在单刀片视图中有两种形式。两者都执行更新功能。

一个表单更新用户,另一个表单更新名为 take_care_persons 的表。

每个用户都有一个看护人。

每当用户试图编辑他/她的个人资料时,他/她也应该能够在需要时更新他/她的看护人。

以下是我的刀片视图。我在这里只包括表格,但请注意两种表格都在同一个视图中。

用户更新表格

{!! Form::model($user, ['method' => 'PATCH','enctype'=>'multipart/form-data','route' => ['participants.update', $user->id]]) !!}

<div class="row">
    <div class="col-md-3 mt-5">
        <!-- @if($user->image_id != 'default-avatar.png')
            <button type="submit" name="resetphoto" class="btn btn-danger px-3 mr-5 pull-right"><i class="fa fa-trash" aria-hidden="true"></i>
            </button>
        @endif
         -->
        @if(empty($user->image_id))
            <p>
                <center>
                    <img src="/propics/default-avatar.png" alt="Profile Pic" id="profile_pic_display_settings" class="mb-3">
                </center>
            </p>
        @else
            <?php
            if(file_exists('propics/'.$user->image_id.'')) {
                echo '<center> <img src="/propics/'.$user->image_id.'" alt="Profile Pic" id="profile_pic_display_settings" class="mb-3"></center>';
            } else {
                echo '<center> <img src="/propics/default-avatar.png" alt="Profile Pic" id="profile_pic_display_settings" class="mb-3"></center>';    
            }?>
        @endif 
        <center>
            <label for="propic" class="btn btn-default subscribe"><i class="fas fa-camera"></i></label>
        </center>
        <input id="propic" type="file" name="image_id" class="form-control" onchange="loadFile(event)">
        <center>
            <label id="file_name"></label>
        </center>
        @if ($tcpSession=='1')
            @error('image_id')
                <span class="help-block" role="alert">
                    <strong></strong>
                </span>
            @enderror
        @elseif($tcpSession=='2')
            @error('image_id')
                <span class="help-block" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror
        @endif    
    </div>
    <div class="col-md-9 mt-5">
        <div class="form-group row">
            <div class="col-md-6">   
                {!! Form::text('first_name', null, array('placeholder' =>  __('texts.first name'),'class' => 'form-control txt_txt')) !!}
                {!! $errors->first('first_name', '<span class="help-block" role="alert">:message</span>') !!} 
            </div>
            <div class="col-md-6">  
                {!! Form::text('last_name', null, array('placeholder' =>  __('texts.last name'),'class' => 'form-control txt_txt')) !!}
                {!! $errors->first('last_name', '<span class="help-block" role="alert">:message</span>') !!}
            </div>
        </div>
        <div class="form-group row">
            <div class="col-md-6 ">
                {!! Form::text('email', null, array('placeholder' => 'Email','class' => 'form-control txt_txt')) !!}
                {!! $errors->first('email', '<span class="help-block" role="alert">:message</span>') !!}
            </div>
            <div class="col-md-6 ">
                {!! Form::password('password', array('placeholder' =>  __('texts.password'),'class' => 'form-control txt_txt')) !!}
                {!! $errors->first('password', '<span class="help-block" role="alert">:message</span>') !!}
            </div>
        </div>
        <div class="form-group row">
            <div class="col-md-6 ">
                {!! Form::password('confirm-password', array('placeholder' =>  __('texts.confirm password'),'class' => 'form-control txt_txt')) !!}
            </div>
            <div class="col-md-6">
                <!-- {!! Form::select('roles[]', $roles,$userRole, array('class' => 'form-control','multiple','style'=>'height:100px')) !!} -->
                {{ Form::select('roles', $roles , $userRole,array('class' => 'form-control txt_txt')) }}                       
                {!! $errors->first('roles', '<span class="help-block" role="alert">:message</span>') !!}
                {!! Form::text('role_id','null', array('class' => 'form-control txt_none')) !!}
                {!! Form::text('gender','null', array('class' => 'form-control txt_none')) !!}
            </div>
        </div>
        <div class="form-group row">
            <div class="col-md-6 ">
                {!! Form ::radio('gender','M', (Request::old('gender') == 'M'),array('class' => 'txt_txt')) !!}
                Male
                {!! Form ::radio('gender','F', (Request::old('gender') == 'F'),array('class' => 'txt_txt')) !!}
                Female
                {!! Form ::radio('gender','O', (Request::old('gender') == 'O'),array('class' => 'txt_txt')) !!}
                Other
                {!! $errors->first('gender', '<span class="help-block" role="alert">:message</span>') !!}
            </div>
            <div class="col-md-6">
                {{ Form::select('region_id', $region_options , Request::old('region_id'),array('class' => 'form-control txt_txt')) }}
                {!! $errors->first('region_id', '<span class="help-block" role="alert">:message</span>') !!}
            </div>
        </div>
        <div class="col-md-12 text-right px-0 ">
            <a class="btn btn-primary btn-admin-form-cancel mt-5" href="{{ route('participants.index') }}"> {{ __('texts.Cancel') }}</a>
            <button type="submit" class="btn btn-primary btn-admin-form-save mt-5">{{ __('texts.Save') }}</button>
        </div>
    </div>
</div>
{!! Form::close() !!}

照顾人更新表格

{!! Form::model($tcp, ['method' => 'PATCH','enctype'=>'multipart/form-data','route' => ['participants.updatetcp', $tcp->id]]) !!}

<div class="row mb-5">
    <div class="col-md-12 mb-5">
        <h2 class="view-title">
            {{ __('texts.Taking Care of') }}
        </h2>
    </div>
    <div class="col-md-3">
        @if(empty($tcp->image_id))     
            <center>
                <img src="/propics/default-avatar.png" alt="Profile Pic" id="profile_pic_display_settings" class="mb-3">
            </center>
            </p>
        @else
            <?php
            if(file_exists('propics/'.$tcp->image_id.'')){
                echo '<center> <img src="/propics/'.$tcp->image_id.'" alt="Profile Pic" id="tcp_img" class="mb-3"></center>
                ';
            } else {
                echo '<center> <img src="/propics/default-avatar.png" alt="Profile Pic" id="tcp_img" class="mb-3"></center>';    
            }?>
        @endif 
            <center>
                <label for="propictcp" class="btn btn-default subscribe"><i class="fas fa-camera"></i></label>
            </center>
            <input id="propictcp" type="file" name="image_id" class="form-control" onchange="loadFileTcp(event)">
            <center><label id="file_nametcp"></label></center>
        @if ($tcpSession=='2')
            @error('image_id')
                <span class="help-block" role="alert">
                    <strong></strong>
                </span>
            @enderror
        @elseif($tcpSession=='1')
            @error('image_id')
                <span class="help-block" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror
        @endif
    </div>
    <div class="col-md-9 mt-5">
        <div class="form-group row">
            <div class="col-md-6">   
                {!! Form::text('first_name', null, array('placeholder' =>  __('texts.first name'),'class' => 'form-control txt_txt')) !!}
                {!! $errors->first('first_name', '<span class="help-block" role="alert">:message</span>') !!} 
            </div>
            <div class="col-md-6">  
                {!! Form::text('last_name', null, array('placeholder' =>  __('texts.last name'),'class' => 'form-control txt_txt')) !!}
                {!! $errors->first('last_name', '<span class="help-block" role="alert">:message</span>') !!}
            </div>
        </div>
        <div class="form-group row">
            <div class="col-md-6">   
                {!! Form::text('date_of_birth', null, array('placeholder' =>  __('texts.Date of birth'),'class' => 'form-control txt_txt')) !!}
                {!! $errors->first('date_of_birth', '<span class="help-block" role="alert">:message</span>') !!} 
            </div>
            <div class="col-md-6">   
                {!! Form::text('user_id', $user->id, array('placeholder' => 'User ID','class' => 'form-control txt_txt d-none')) !!}
                {!! $errors->first('user_id', '<span class="help-block" role="alert">:message</span>') !!} 
            </div>
        </div>
        <div class="col-md-12 text-right px-0 ">
            <a class="btn btn-primary btn-admin-form-cancel mt-5" href="{{ route('participants.index') }}"> {{ __('texts.Cancel') }}</a>
            <button type="submit" class="btn btn-primary btn-admin-form-save mt-5">{{ __('texts.Save') }}</button>
        </div>
    </div>
</div>
{!! Form::close() !!}

现在我的控制器看起来像这样。我在这里只包括相关的两个更新功能。

public function edit($id) {
    if(!empty($id)) {
        $user = User::find($id);
        if(empty($user)) {
            abort(403, 'No such an user exists');
        } else {
            $tcp = TakeCarePerson::WHERE('user_id','=',$id)->first();
            $region_options = Region::orderBy('region_id', 'asc')->pluck('name','region_id');
            $roles = Role::pluck('name','name')->all();
            $userRole = $user->roles->pluck('name','name')->all();
            return view('admins.participants.edit',compact('user','roles','userRole','tcp','region_options'));
        }
    } else {
        abort(403, 'No such an user exists');
    }
}
/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id) {
    try {
        $roleid = $request->input('roles');
        if(empty($roleid)) {
            $roleid='3';
               
            } else {
                $roleid=$request->input('roles');
                if($roleid=='Admin') {
                    $roleid='1';    
                } else if($roleid=='Regional Admin') {
                    $roleid='2'; 
                } else {
                    $roleid='3'; 
                }
            }
         
            //dd($roleid);
            Session::put('tcpSession', '2');
            $request->merge(['role_id' => ''.$roleid.'']);
            $request->merge(['date_of_birth' => '1992-01-11']);
            $this->validate($request, [
                'first_name' => 'required',
                'last_name' => 'required',
                'email' => 'required|email|unique:users,email,'.$id,
                'password' => 'same:confirm-password',
                'roles' => 'required',
                'image_id' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
                'role_id'=>'required',
                'region_id'=>'required',
                'gender'=>'required',
                'date_of_birth'=>'required'
            ]);
            $input = $request->all();
            if ($image = $request->file('image_id')) {
                $destinationPath = 'propics/';
                $profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
                $image->move($destinationPath, $profileImage);
                $input['image_id'] = "$profileImage";
            } else {
                unset($input['image']);
            }
            if(!empty($input['password'])) {
                $input['password'] = Hash::make($input['password']);
            } else {
                $input = array_except($input,array('password'));    
            }
            $user = User::find($id);
            $user->update($input);
            DB::table('model_has_roles')->where('model_id',$id)->delete();
            $user->assignRole($request->input('roles'));
            return redirect()->route('participants.index')
                ->with('success',__('texts.Player updated successfully.'));
        } catch(Exception $e) {
            return redirect()->route('participants.index')
                ->with('failed',__('texts.An error has been occured.'));
        }
    }

护理人员的功能如下,

public function edittcp($id) {
    $tcp = TakeCarePerson::WHERE('user_id','=',''.$id.'');
    return view('admins.participants.edit',compact('tcp'));
}
/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function updatetcp(Request $request, $id) {
    try {
        Session::put('tcpSession', '1');
        $request->merge(['gender' => 'M']);
        $this->validate($request, [
            'first_name' => 'required',
            'last_name' => 'required',
            'image_id' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048',
            'user_id'=>'required',
            'gender'=>'required',
            'date_of_birth'=>'required'
        ]);
        //$input = $request->except('_method', '_token');
        $input = $request->all();
        unset($input['_token']);
        unset($input['_method']);
        if ($image = $request->file('image_id')) {
            $destinationPath = 'propics/';
            $profileImage = date('YmdHis') . "." . $image->getClientOriginalExtension();
            $image->move($destinationPath, $profileImage);
            $input['image_id'] = "$profileImage";
            //dd($profileImage);
        } else {
            unset($input['image']);
        }
        $tcp = TakeCarePerson::WHERE('id','=',''.$id.'');
        $tcp->update($input);
        return redirect()->route('participants.index')
            ->with('success',__('texts.Take care person updated successfully.'));
        } catch(Exception $e) {
            return back() ->with('failedTcp',__('texts.Le fichier sélectionné doit être une image.'));
        }
    }

但现在我的问题是,每当我尝试用错误的输入更新我的护理人员时,它都会验证,并且一旦验证它也会用护理人员的旧值填充用户表单字段!

我怎样才能避免这种情况并用正确的旧数据填写两个表格。

4

0 回答 0