仅在更改所有字段时更新表

时间:2017-07-13 01:29:32

标签: laravel laravel-5

如果我只更改user表项,它将更新它们(如果我更改user表行中的所有内容)。但是,它只会在我编辑所有内容时更新user_infos表。

ProfilesController.php

  public function update(Request $request)
  {
      $user_id = Auth()->user()->id;
      $id = Auth()->user()->id;

      $this->validate($request, array(
        'first_name'  => 'required|string|max:255',
        'last_name'   => 'required|string|max:255',
        'email'       => 'required|string|email|max:255|unique:users',
        'type'        => 'required|string',
        'description' => 'string',
        'projects'    => 'string',
        'experience'  => 'string',
        'links'       => 'string',
        'status'      => 'string'
      ));

      if($request->hasFile('avatar')){
          $avatar = $request->file('avatar');
          $filename = time() . '.' . $avatar->getClientOriginalExtension();
          Image::make($avatar)->resize(300, 260)->save( public_path('/images/uploads/avatars/' . $filename ) );

          $user = Auth::user();
          $user->avatar = $filename;
          $user->save();
      }


      $user = User::find($id);
      $user->first_name = $request->input('first_name');
      $user->last_name  = $request->input('last_name');
      $user->email      = $request->input('email');
      $user->type       = $request->input('type');


      $user_info = User_Info::find($user_id);
      //$user_info = User_Info::where('user_id', $user->id)->first();
      $user_info->description = $request->input('description');
      $user_info->projects    = $request->input('projects');
      $user_info->experience  = $request->input('experience');
      $user_info->links       = $request->input('links');
      $user_info->status      = $request->input('status');


      $user->save();
      $user_info->save();


      return redirect()->route('profile')->withUser($user);
  }

settings.blade.php

   <div class="twelve wide column">
      <div class="ui segment" data-tab="bio">

        <form action="{!! action('ProfilesController@update', ['id' => $user->id]) !!}" method="POST" enctype="multipart/form-data" class="ui form">
          {{ csrf_field() }}

          <h3>Name</h3>
          <div class="inline fields">
            <div class="eight wide field">
              <input type="text" name="first_name" placeholder="First Name" value="{{ $user->first_name }}">
            </div><!-- ./Eight Wide Field -->
            <div class="eight wide field">
              <input type="text" name="last_name" placeholder="Last Name" value="{{ $user->last_name }}">
            </div> <!-- ./Eight Wide Field -->
          </div><!-- ./Inline Fields -->

          <h3 class="header">Profile Image</h3>
          <div class="inline fields">
            <div class="sixteen wide field">
              <input type="file" name="avatar">
            </div> <!-- ./Sixteen Wide Field -->
          </div> <!-- ./Inline Fields -->

          <h3 class="header">Email</h3>
          <div class="inline fields">
            <div class="sixteen wide field">
              <input type="email" name="email" placeholder="Email" value="{{ $user->email }}">
            </div> <!-- ./Sixteen Wide Field -->
          </div> <!-- ./Inline Fields -->

          <h3>Type</h3>
          <div class="inline fields">
            <div class="sixteen wide field fluid">
              <select name="type" class="ui dropdown fluid registerType">
                <option value="Developer">Developer</option>
                <option value="Designer">Designer</option>
                <option value="Fullstack">FullStack</option>
                <option value="Client">Client</option>
              </select>
            </div> <!-- ./Sixteen Wide Field Fluid -->
          </div><!-- ./Inline Fields -->

          <h3 class="header">Description</h3>
          <textarea name="description" id="" cols="30" rows="10" placeholder="Description">{{ isset($user->userInfo->description) ? $user->userInfo->description : "This User Has No Description" }}</textarea>

          <h3 class="header">Projects</h3>
            <textarea name="projects" id="" cols="30" rows="3" placeholder="Projects">{{ isset($user->userInfo->projects) ? $user->userInfo->projects : "This User Has No Projects Listed" }}</textarea>

          <h3 class="header">Experience</h3>
            <textarea name="experience" id="" cols="30" rows="3" placeholder="Experience">{{ isset($user->userInfo->experience) ? $user->userInfo->experience : "This User Has No Experience Listed" }}</textarea>

          <h3 class="header">Links</h3>
            <input type="text" name="links" placeholder="Links" value="{{ isset($user->userInfo->links) ? $user->userInfo->links : "This User Has No Links Listed" }}">

          <h3 class="header">Status</h3>
            <input type="text" name="status" placeholder="Status" value="{{ isset($user->userInfo->status) ? $user->userInfo->status : "Available" }}">

          <br><br>
          <input type="hidden" name="_token" value="{{ csrf_token() }}">
          <input type="submit" class="ui medium fluid blue button" value="Update">
        {!! Form::close() !!}
      </div><!-- ./Ui Segment -->
    </div> <!-- ./Twelve Wide Column -->

提前致谢! 使用:Laravel 5.4

2 个答案:

答案 0 :(得分:0)

在这里:

$user->update(); // <--- try this
$user->save();
$user_info->save();

答案 1 :(得分:0)

在我的ProfilesController.php我必须稍微更改public function update(Request $request)内的“电子邮件”验证。

来自:

$this->validate($request, array(
        'first_name'  => 'required|string|max:255',
        'last_name'   => 'required|string|max:255',
        'email'       => 'required|string|email|max:255|unique:users',
        'type'        => 'required|string',
        'description' => 'string',
        'projects'    => 'string',
        'experience'  => 'string',
        'links'       => 'string',
        'status'      => 'string',
      ));

要:

$this->validate($request, array(
        'first_name'  => 'required|string|max:255',
        'last_name'   => 'required|string|max:255',
        'email'       => 'required|string|email|max:255|unique:users,email,'.$id,
        'type'        => 'required|string',
        'description' => 'string',
        'projects'    => 'string',
        'experience'  => 'string',
        'links'       => 'string',
        'status'      => 'string',
      ));