出于某种原因,当提交我的`users`表时根本没有更新,我的`user_infos`表添加了新列而不是更新

时间:2017-07-09 17:34:13

标签: laravel laravel-5

这是我的商店功能。我试过用几种不同的方式玩它。出于某种原因,我想可能会把$ post-> save();在if里面至少会改变个人资料图片,但是没有。甚至不行。我最初尝试返回redirect() - > route(' profile') - > withUser($ user);那也没有用。这是我最近的尝试。仍然没有运气,所以我想在这里发布。提前致谢! 使用:Laravel 5.4

    public function store(Request $request)
  {
    $this->validate($request, array(
      'description' => 'required|string',
      'projects'    => 'required|string',
      'experience'  => 'required|string',
      'links'       => 'required|string',
      'status'      => 'required|string',
    ));

    $userInfo = new User_Info;
    $user_id = Auth()->user()->id;
    $user = User::find($user_id);

    $userInfo->user_id = $user_id;
    $userInfo->description = $request->input('description');
    $userInfo->projects = $request->input('projects');
    $userInfo->experience = $request->input('experience');
    $userInfo->links = $request->input('links');
    $userInfo->status = $request->input('status');

    $userInfo->save();

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

  public function settings($id)
  {
    $user = User::find($id);
    return view("profile.settings")->withUser($user);
  }

  public function update(Request $request, $id)
  {
      $user_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' => 'required|string',
        'projects'    => 'required|string',
        'experience'  => 'required|string',
        'links'       => 'required|string',
        'status'      => 'required|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 = 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 = DB::select("SELECT * FROM user_infos WHERE user_id = $user_id");
      $user_info->description = $request->input('description');
      $user_info->projects    = $request->input('projects');
      $user_info->expereince  = $request->input('experience');
      $user_info->status      = $request->input('status');


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

      return view('profile.profile')->withUser($user);
  }

1 个答案:

答案 0 :(得分:1)

您没有获得现有对象,因此请更改:

$user_info = DB::select("SELECT * FROM user_infos WHERE user_id = $user_id");

对于这样的事情:

$user_info = UserInfo::where('user_id', $user->id)->first();