使用laravel中的用户ID更新值

时间:2017-08-17 07:16:16

标签: php laravel

如何使用用户ID更新值。控制器,布局和路由如下所示:

Controller.php这样

public function profileedit() {

    $id = \Auth::user()->id; 
    $user_id = \Auth::user()->user_id;

    $teacheredit = DB::table('teachers')
    //->join('campaign_type', 'campaign.campaign_id', '=', 'campaign_type.id')
    //->select('campaign.*', 'campaign_type.*','campaign.id as campaignid','campaign.status as campaign_status')
    ->select('teachers.*', 'teachers.id as teacher_id')
     ->where('teachers.id', '=', $user_id)
     ->get(); 

    return view('layouts.teacher.profile', array('teacheredit' => $teacheredit));
}

public function profileupdate($ids, Request $request) {
    $profile = Teachers::find($ids);
    $id = \Auth::user()->id;
    $user_id = \Auth::user()->user_id;

    $subscriber_id = \Auth::user()->subscriber_id;
    $inputteacher = $request->all();

        $profileeditdata = array( 
            'teacherFirstName' => $inputteacher['teacher_firstname'],
            'teacherLastName' => $inputteacher['teacher_lastname'], 
        );
   //dd($profileeditdata);

        $profile->fill($profileeditdata)->save();  
    // \Session::flash('success', 'Data Insert Successfully!');
    return Redirect('/teacher/profile');
}

在这种情况下编辑正在运行,但更新无效,请向我提供帮助。

profile.php

@extends('layouts.default')
@section('content')
<div id="createStudent" type="view" class="demo-section k-header">   
  @include('layouts.common.flash-message')
  {!! Form::open(array('url' => 'profileupdate/'.$teacheredit[0]->teacher_id,'class'=>'horizontal-form'))!!}

<form id="createTeacherForm" method="post" action="../teacherregistration" >

<ul id="fieldlist" >
            <li>
            <label style="color:Green;font-size:15px">Profile</label>
            </li>
            <li>
            <table id="lessonPlanTable">
                 <tr>
                     <td><label> UserType  </label> </td>
                     <td><label> <?= $teacheredit[0]->teacher_firstname ?>  </label></td>
                  </tr>
                 <tr>
                  <td><label> First Name  </label> </td>
                  <td><input type="text" id="teacher_firstname" name="teacher_firstname" value="<?php echo $teacheredit[0]->teacher_firstname ?>" class="k-textbox"/></td>
                  </tr>
                  <tr>
                  <td><label> Last Name </label> </td>
                  <td><input  type="text" id="teacher_lastname" name="teacher_lastname" value="<?php echo $teacheredit[0]->teacher_lastname ?>" class="k-textbox"/></td>
                  </tr>

                 <tr>
                  <td><label> Email </label> </td>
                  <td><input type="email" id="teacherEmail" name="teacherEmail" value="<?php echo $teacheredit[0]->teacher_email ?>" class="k-textbox"/ readonly></td>
                  </tr>
                  </table>
                </li>
        <li><br>
            <button id="updateTeacherProfile" class="k-button k-primary" type="submit">Update</button>&nbsp;&nbsp;
            <button id="resetTeacherPassword" class="k-button k-primary" type="button" style="width:20%">Reset Password</button>
        </li>
        <br><br>

    </ul>
</form>

</div>
 @stop

Route.php

Route::get('/teacher/profile', 'Teacher\TeacherController@profileedit'); 
Route::post('profileupdate/{id}', 'Teacher\TeacherController@profileUpdate');

编辑完全正常工作没有任何错误但在更新中我使用dd()检查答案它返回正确答案但隐藏dd()后结果将不会更新..

1 个答案:

答案 0 :(得分:2)

尝试使用以下代码更新记录:

$profile = Teachers::find($ids);
$profile->teacherFirstName = $inputteacher['teacher_firstname'];
$profile->teacherLastName = $inputteacher['teacher_lastname'];
$profile->save(); 

注意:删除了fill(),因为您的下一个方法profileedit()已经选择了相同的记录。