douwei1921 2018-05-15 16:58
浏览 86

如何更新laravel中的users表?

I want to update the value of one column of the 'users' table. This is the code of the controller:

<?php


class RankController extends Controller
{
    public function index()
    {
    //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {   
        // Create an array of all  users
        $users = User::all();
        // Return the view of change rank with users
        return view('rank.edit')->with('users', $users);
    }


    public function store(Request $request)
    {
       $this->validate($request, [
            'rank' => 'required',
            'user_id' => 'required',
     ]);
     $id = $request->input('user_id');
     $user = User::find($id);
     $user->rank = $request->input('rank');
     $user->save();


     return redirect('../public/home')->with('succes', 'The rank id is'.$id.'         '.$user.'<br>'.'<br>'.$user->rank);

      }

    public function show($id)
    {
        //
    }


    public function edit($id)
    {

    }


    public function update(Request $request, id)
    {
        //
    }


    public function destroy($id)
    {
        //
    }


}

After checking the variables I find the problem: 'save()' method doesn't work, and I cant't figure out why it doesn't work. Any ideas why doesn't work? Thanks in advance!

  • 写回答

1条回答 默认 最新

  • druybew06513 2018-05-15 17:03
    关注

    Unless you've changed your App\User model to be keyed off of the user_name, you're searching for the user incorrectly.

    $username = $request->input('user_name');
    
    $user = User::where('user_name', $username)->firstOrFail();
    
    // ...
    

    find() searches by the primary key, which is the id column by default.

    评论

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)