如何使用此代码管理Laravel 5 FrameWork - MVC中的帐户?

时间:2017-05-08 21:11:05

标签: laravel-5

如何使用此代码管理Laravel 5 FrameWork - MVC中的帐户?我得到了一个默认的演示文稿,但我仍然得到一个未定义的变量请求与此代码 - 请您的答案将不胜感激:

UserController中:

 public function account(&$data, $request){
    User::get_user(self::$data,$request);
    self::$data['title'] = self::$data['title'] . 'Edit Account';
    return view('forms.account', self::$data);
}

public function postAccount(AccountRequest $request){
    User::edit_user($request);
    return redirect('');
}

AccountRequest:

public function rules()
{
    return [
        'name' => 'required|min:2|max:70',
        'email' => 'required|email|unique:users,email',
        'password' => 'required|min:6|max:10|confirmed',
    ];
}

型号:

 static public function get_user(&$data,$request){
    $sql = "SELECT * FROM users WHERE id = ". Session::get('user_id');
    $data['users'] = DB::select($sql);
}

static public function edit_user(&$data,$request) {

    $id = $data['id'];
    $sql = "SELECT * FROM users WHERE id = ".$id;
    $getVal = DB::select($sql);
    if($data['name'] || $data['password'] || $data['email']){
        if($data['name']){
            DB::update("UPDATE users SET name = ? WHERE id = ?",[$data['name'],$id]);
            session(['user_name' => $data['name']]);
        }
        if($data['password']){
            DB::update("UPDATE users SET password = ? WHERE id = ?",[bcrypt($data['password']),$id]);
        }
        if($data['email']){
            DB::update("UPDATE users SET email = ? WHERE id = ?",[$data['email'],$id]);
        }
    }

    Session::flash('sm',$request['name'] . '`s Has Been Updated');
}

网络:     Route :: get(' user / account',' UserController @ account');     Route :: post(' user / account',' UserController @ postAccount');

HTML:

@extends('master')

@section('content')
<div class="container">
<div class="row">
<div class="col-md-12 text-center">
<h1>Edit Your Account -</h1>
</div>
<div class="row" style="margin-left:30%;">
  <div class="col-md-6">
    <form action="" method="post">
     {{ csrf_field() }}
     <input type="hidden" name="user_id" value="{{ $user['id'] }}">
     <div class="form-group">
       <label for="name"></label>
       <input value="{{ $user['name'] }}" type="text" name="name" 
 class="form-control" id="name" placeholder="Name">
      </div>
      <div class="form-group">
       <label for="email"></label>
       <input value="{{ $user['email'] }}" type="text" name="email" 
 class="form-control" id="email" placeholder="Email">
       </div>
       <div class="form-group">
         <label for="editpassword"></label>
         <input type="password" name="password" class="form-control" 
  id="editpassword" placeholder="Edit Password">
     </div>
     <div class="form-group">
       <label for="editpasswordconf"></label>
       <input type="password" name="password_confirmation" class="form-
 control" id="editpasswordconf" placeholder="Confirm New Password">
     </div>
     <div class="form-group text-center">
     <input type="submit" name="submit" value="Update Details" class="btn 
btn-primary">
     </div>
    </form>
    </div>
  </div>
</div>
@endsection

1 个答案:

答案 0 :(得分:0)

您的AccountController应如下所示:

public function edit($request, Account $account){
  return view('forms.account', [
    'account' => $account,
    'title' => 'Edit Account'
  ]);
}

public function update(AccountRequest $request, Account $account){
  $account->update($request->all());
  Session::flash('sm', $account->name . ' Has Been Updated');
  return redirect()->back();
}

AccountRequest:

public function rules()
{
  return [
    'name' => 'required|min:2|max:70',
    'email' => 'required|email|unique:users,email',
    'password' => 'required|min:6|max:10|confirmed',
  ];
}

这个代码与您需要的代码一样多...请阅读Eloquent used

的文档